2

I am using Asp.Net/C# in my project.I am using Forms Authentication.Currently I call FormsAuthentication.SignOut() on click of Asp.Net button.It works well , but my requirement is that I need to log out a user from horizontal menu option

Logout

That would be Html menu , so how do I call FormsAuthentication.SignOut() method when a user clicks Logout from the menu bar.Is there any solution possible or any other technique. Any suggestions are welcome. Thanks

2 Answers 2

4

FormsAuthentication.SignOut() is a server side code and you cannot directly invoke it from client side (i.e. browser) - instead, you need to create a URI that will do the same for you.

For example, you can have AJAX service that will call above code or simply speaking, you can have logout.aspx page that will call FormsAuthentication.SignOut() in say page_load. Such a URI can be invoked from jquery to get you what you want.

In your case, you should simply have a link to logout aspx page in your logout menu (or in menu click, write document.location = "/logout.aspx" which essentially means navigating to log-out page).

Sign up to request clarification or add additional context in comments.

3 Comments

,thank you sir for making me understand it.What I would like to do is when the user clicks the logout link I need to redirect him to Login.aspx , my starting page.So on the page_load of Logout.aspx I must call the FormsAuth.Signout() method and then use Response.Redirect("Login.aspx"); , is it a proper approach.Thanks
@freebird, yes - that would do. Another approach is to pass some query parameter to login page (e.g. login.aspx?new_session) so that login page it self can call FormsAuth.Signout() - this will avoid one (unnecessary) server trip!
@VinayC Best idea so far. Clean with no extra load.
1

here you go:

    $('#logout').click(function () {
    $.ajax({
        url: '/logout',
        success: function () {
            document.location = '/logged_out';
        }, error: function () { 
            alert('Logout failed');
        }
    });
    return false;
  });

2 Comments

thanks for that could you please explain me about the url: and document.location , never used ajax before.thanks
document.location: would redirect to the specified url. If you are really a newbie then you need to have jquery library first: please visit this link to learn more:dotnetcurry.com/ShowArticle.aspx?ID=231

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.