Wednesday, August 1, 2012

Logout User In Asp.net Membership

While developing usermanagement based web application using Membership provider, many times we come across some trivial issues or queries as follows
  • formsauthentication.signout not working
  • Using formsauthentication.signout
  • logout user programmatically
  • Check user date subscription and log out if user is not valid.
  • programmatically logout in asp net
  • Logout user in asp.net Membership
Solution for all above queries is as follows
FormsAuthentication.SignOut();
  Session.Abandon();

 // clear authentication cookie
 HttpCookie frmcookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
 frmcookie.Expires = DateTime.Now.AddYears(-1);
 Response.Cookies.Add(frmcookie);

 // clear session cookie if we use any
 HttpCookie sessioncookie = new HttpCookie("ASP.NET_SessionId", "");
 sessioncookie.Expires = DateTime.Now.AddYears(-1);
 Response.Cookies.Add(sessioncookie);

 FormsAuthentication.RedirectToLoginPage("v=SubscriptionExpired");
Above code signout the user, remove all the Cookies set. Forms Authentication.RedirectToLoginPage uses settings in web.config file. We can pass Querystring to the function so that we can show proper Messages.
Submit this story to DotNetKicks

0 comments: