BeanSoftware Logo
 

 
ASP.NET Database Search Control
 
 

 
 Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

How to Logout User in ASP.NET Membership?

If you use ASP.NET membership, then you need an option to logout user on click to Logout button. To avoid repetitive code you can place this in master page. Here is an example:

[ C# ]

using System;
 
// We need this namespace to use FormsAuthentication class
using System.Web.Security;
 
public partial class MasterPages_DeafultMasterPage : System.Web.UI.MasterPage
{
 
  protected void btnLogout_Click(object sender, EventArgs e)
  {
    // Sign out
    FormsAuthentication.SignOut();
    // Now need to redirect user to login page 
    // or to the same URL
    Response.Redirect(Request.Url.AbsoluteUri);
  }
}

[ VB.NET ]

' We need this namespace to use FormsAuthentication class
Imports System.Web.Security
 
 
Partial Class VB_DefaultMasterPage
  Inherits System.Web.UI.MasterPage
 
  Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
    ' Sign out
    FormsAuthentication.SignOut()
    ' Now need to redirect user to login page 
    ' or to the same URL
    Response.Redirect(Request.Url.AbsoluteUri)
  End Sub
End Class

The above code will logout current user and redirect to same page. Instead of that, you can redirect user to login page. Replace line:

Response.Redirect(Request.Url.AbsoluteUri);

with

FormsAuthentication.RedirectToLoginPage();

Of course, you can also use Response.Redirect to redirect to Login page in form like Response.Redirect("~/Login.aspx") but this way with RedirectToLoginPage() method looks easier and less hard coded.



Related articles:

1. How to Find If User Is Logged In?
2. How To Change User's Email In ASP.NET Membership?

FAQ toolbar: Submit FAQ  |  Tell A Friend  |  Add to favorites  |  Feedback



Copyright © 2002-2008 Bean Software. All rights reserved.