Shining Star Services LLC

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

by Nannette Thacker

Our Log In Page.

On our Log In Page, we use a custom Remember Me checkbox instead of the checkbox included with the log in control. This way we can remove our log in cookie ourselves and do any database changes needed to log the user out.



Since we are using a Log In control, notice in our codebehind how we retrieve the Remember Me cookie using the FindControl to find our Login control and then the FindControl again to find the RememberMe checkbox within the Login control. Also, notice how we must find the UserName field in order to set focus on that control.

Log In Code in Front

<%@ Page Language="VB" MasterPageFile="~/SSS.master" AutoEventWireup="false"
    CodeFile="Login.aspx.vb" Inherits="LoginClass" Title="Shining Star Services Login" %>
<%@ MasterType VirtualPath="~/SSS.Master" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="Server">
    <div>
        <asp:LoginView ID="LoginView1" runat="server">
            <LoggedInTemplate>
                You are logged in as
                <asp:LoginName ID="LoginName1" runat="server" />
                <br />
            </LoggedInTemplate>
            <AnonymousTemplate>
                <!-- nkt: use AutoEventWireup="false" for better server performance -->
                <!-- nkt: OnLoggingIn Event is wired up in the control -->
                <asp:Login ID="Login1" runat="server" 
                    OnLoggingIn="Login1_LoggingIn" DestinationPageUrl="~/default.aspx"
                    >
                    <LayoutTemplate>
                        <asp:Panel runat="server" ID="LoginContainer" DefaultButton="LoginButton">
                            <table border="0" cellpadding="0" width="100%">
                                <tr>
                                    <td align="center" colspan="2">
                                        Log In</td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        If you are not yet a member, please
                                        <asp:HyperLink ID="Register" runat="server" NavigateUrl="~/login/Register.aspx">Register</asp:HyperLink>
                                        for a free account.
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" style="width: 40%">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Log In/User  Name:</asp:Label></td>
                                    <td align="left" style="width: 60%">
                                        <asp:TextBox ID="UserName" runat="server" Font-Size="1em"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
                                            ControlToValidate="UserName" ErrorMessage="Log In ID is required."
                                            ToolTip="Log In ID is required." ValidationGroup="ctl01$Login1"
                                            ForeColor="DarkRed">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                                    <td align="left" style="width: 178px; height: 20px">
                                        <asp:TextBox ID="Password" runat="server" Font-Size="1em" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
                                            ControlToValidate="Password" ErrorMessage="Password is required."
                                            ToolTip="Password is required." ValidationGroup="ctl01$Login1"
                                            ForeColor="DarkRed">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="center">
                                        <asp:CheckBox ID="RememberMeCustom" runat="server" Text= "Check this box to keep you logged in on this computer." />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login"
                                            Text="Log In" ValidationGroup="ctl01$Login1" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:HyperLink ID="Logout" runat="server" NavigateUrl="~/login/logout.aspx">Click Here</asp:HyperLink>
                                        to Remove Your Login Cookie
                                    </td>
                                </tr>
                            </table>
                        </asp:Panel>
                    </LayoutTemplate>
                </asp:Login>
            </AnonymousTemplate>
        </asp:LoginView>
        <p>
        </p>
    </div>
</asp:Content>

Log In Code Behind

Imports SSSCookie ' for storing and retrieving cookies
Imports FormUtility ' form utility functions


Partial Class LoginClass
    Inherits System.Web.UI.Page

    ' http://msdn2.microsoft.com/en-us/library/ms178472.aspx
    ' ASP.NET Page Life Cycle Overview 

#Region "Page_Load"


    ' http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx
    ' AutoEventWireup and Handles Me.Load
    ' nkt: if AutoEventWireup is set to False, it won't run the Page_Load without Handles Me.Load

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

        If Not IsPostBack Then

            If Not User.Identity.IsAuthenticated = True Then
                ' nkt: below code sets the focus to the first field in the control
                'LoginView1.FindControl("Login1").Focus()
                ' use the below to find the UserName and set focus on a specific field by name...
                Dim lctrl As Login
                lctrl = CType(LoginView1.FindControl("Login1"), Login)

                '' nkt: below code sets the focus on the UserName field...
                '' MS built in function doesn't work using master pages and contentplaceholders... it can't find the control
                'lctrl.FindControl("UserName").Focus()
                ' nkt: the above works consistently for IE6 and Firefox, but not IE 7
                ' for IE7 browsers to work consistently, custom write the javascript
                ' if IE7 has a future "fix" should be able to remove this and use the first function
                Dim tb As TextBox = CType(lctrl.FindControl("UserName"), TextBox)
                FormUtility.SetFocusControl(tb)
            End If

        End If

    End Sub

#End Region

#Region "LoggingIn"

    Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
        ' nkt: don't use Handles Login1.LoggingIn in the Sub declaration, 
        '   not needed since control is wired up in the Loginview

        ' since the control is embedded in the Loginview, you must find it first.... this was a bugger.
        Dim lctrl As Login
        lctrl = CType(LoginView1.FindControl("Login1"), Login)

        Dim chk As CheckBox = CType(lctrl.FindControl("RememberMeCustom"), CheckBox)
        If chk.Checked Then
            ' if checked then store as cookie...
            Dim myCookies As New SSSCookie()
            myCookies.SetRememberMeCookie("true")
        End If
    End Sub

#End Region

End Class

We are now ready to begin Step 13.

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.