Shining Star Services LLC

Creating a Custom Membership Provider and Membership User utilizing a Data Set Table Adapter - Step 14

by Nannette Thacker

Our Log Out Page

When the user clicks to Log Out, we'll call our own custom functions to kill the cookies, possibly make database changes, and return them to the Log In page.

The built-in Log Out functionality is overridden in this example so that we can have more control over our cookies and what events actually take place.

Code In Front

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Logout.aspx.vb"
    Inherits="Logout"  Title="Shining Star Services Log Out"%>
<%@ MasterType VirtualPath="~/SSS.Master" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Shining Star Services Log Out</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Thanks for visiting!
        </div>
    </form>
</body>
</html>

Code Behind

Notice my comments on using the Response.Redirects and the ThreadAbortException error. Notice the solution is to pass in false as an extra parameter value:

System.Web.HttpContext.Current.Response.Redirect("~/login/login.aspx", False)

Also, notice my comment on NOT using the FormsAuthentication.RedirectToLoginPage() method and potential problems incurred with it.

Imports SSSCookie

Partial Class Logout
    Inherits System.Web.UI.Page

#Region "Urls"
    ' http://msdn2.microsoft.com/en-us/library/
    'system.web.security.formsauthentication.signout(VS.80).aspx
    ' http://msdn2.microsoft.com/en-us/library/
    ' system.web.security.formsauthentication.redirecttologinpage(VS.80).aspx
#End Region

    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    ' nkt: do NOT log out using the Loginstatus control.
    ' it does not use the custom log out below to kill cookies, etc., 
    ' so use a log in link to this custom page...
    Protected Sub Pre_Init(ByVal sender As Object, _
                           ByVal e As System.EventArgs) Handles Me.PreInit

        If Not User.Identity.IsAuthenticated = True Then
            ' if logged in, do any needed database functions to log out

        End If
        ' clear all cookies
        Dim myCookies As New SSSCookie()
        myCookies.DeleteCookies()
        FormsAuthentication.SignOut()
        System.Web.HttpContext.Current.Response.Redirect("~/login/login.aspx", False)
    End Sub

#Region "Response.Redirects and ThreadAbortException"
    ' A Response.Redirect or Server.Transfer raises a ThreadAbortException, 
    ' since that is what is actually happening. 
    ' (The server aborts the current thread by calling a 
    ' HttpResponse.End and starts executing the new page in a new thread.) 
    ' http://support.microsoft.com/default.aspx?scid=kb;EN-US;312629
    ' For Response.Redirect, use an overload, 
    ' Response.Redirect(String url, bool endResponse) that passes false 
    ' for the endResponse parameter to suppress the internal call to Response.End. For example:
    ' Response.Redirect ("nextpage.aspx", false)
#End Region

#Region "RedirectToLoginPage - do NOT use this built-in function"
    ' nkt:
    ' use this type of redirect (used in code above)
    '    System.Web.HttpContext.Current.Response.Redirect("~/login/login.aspx")
    ' DO NOT USE THE BELOW!
    ' FormsAuthentication.RedirectToLoginPage()   
    ' allow to log in as a different user... uses log in page as setup in web.config
    ' when it redirects you to the Login page, 
    ' every log in attempt fails until you manually click the Log in.
    ' it's a waste of time! don't do it! it'll drive you nuts. Now you know.

#End Region

End Class

We are now ready to begin Step 15.

Steps for Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter:

Introduction: Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter.
Step 1: Creating the Project and Folders.
Step 2: Table Data Structure and Web.config for this Tutorial.
Step 3: Creating The DataSet.
Step 4: Creating the Table Adapter Methods for GetUserByLogin, GetUserByUserName, and InsertUser.
Step 5: Creating our Custom MembershipProvider Class.
Step 6: Adding Properties to Our Custom MembershipProvider Class.
Step 7: Creating Our Custom MembershipUser Class.
Step 8: Customizing our MembershipProvider Class.
Step 9: Our Custom RoleProvider Class.
Step 10: Our Master page.
Step 11: Our Cookie Handler class.
Step 12: Our Log In page.
Step 13: Our Register page.
Step 14: Our Log Out page.
Step 15: Our Change Password page.
Step 16: Our Administration page.
Step 17: Loading a Menu Programmatically based on Roles.
Step 18: Our Default page.
Download the ZIP files:
VB: ShiningStarCustomMemberProviderTutorial.zip
© Copyright 1997-2017 Shining Star Services LLC, Nannette Thacker. All Rights Reserved.