Click here to Skip to main content
Click here to Skip to main content
Go to top

Getting a user's last login date -- Before it's updated by the ASP.NET framework!

, 26 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Retrieve's the user's last login date during login, and store it to a session variable.

Introduction

A quick and easy way to get the user's last login date before the ASP.NET framework updates it during the login process.

How it works

The idea is to catch the Authenticate message before the framework has a chance to do its thing. This will allow us to examine the user's properties before they are actually logged in. Therefore, we are getting the last login date before we authenticate them.

Once, we have a valid user, we store the date into a session variable for later use.

Setting up an example

  • Create a new ASP.NET 2.0 project that uses the Membership Provider. There are lots of samples of this already.
  • Add a login control to your default.aspx page. See the "Login Control" code section below.
  • Verify that this works with your Membership Provider.
  • Add the OnAuthenticate handler to the Login1 control. See "OnAuthenticate Handler" below.
  • In your default.cs file, add the handler function for OnAuthenticate. See the "Authenticate Function" below.

Login control

Add this inside your Default.aspx page's <body> tag:

<asp:Login ID="Login1" runat="server" VisibleWhenLoggedIn="False">
</asp:Login>

OnAuthenticate handler

After adding the OnAuthenticate handler, your login control should look like:

<asp:Login ID="Login1" runat="server" 
   OnAuthenticate="Login1_Authenticate" VisibleWhenLoggedIn="False">
</asp:Login>

Authenticate function

Add the following code inside the partial class in default.cs:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    Login login = (Login)sender;
    // Get the user attempting to login

    MembershipUser user = Membership.GetUser(login.UserName);

    if ( user != null )
    {
        // Get the date the user last logged in before
        // the user is validated and the login gets updated
        // to the current login date

        DateTime dtLastLogin = user.LastLoginDate;

        // Attempt to validate user -- if success then set the authenticated
        // flag so that the framework knows to allow the user access
        if (Membership.ValidateUser(login.UserName, login.Password))
        {
            Session["LastLoginDate"] = dtLastLogin;
            e.Authenticated = true;
        }
    }
}

Conclusion

This is an easy but effective way to get the user's actual last login date.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

aggie_2000
Web Developer
United States United States
Current Position:
Senior Develper
Heat Transfer Research, Inc.
 
Education:
B.S. in Computer Science
Texas A&M University 2001
 
Work Experience:
Devolop and manage projects using C++, C#, and ASP.NET.

Comments and Discussions

 
Generaldetermining last login date for a current user in MOSS 2007 Pinmemberfastian133528-Jun-10 22:22 
Generalthis.Login1.UserName is good! Pinmemberpccai16-Jul-07 5:56 
GeneralNewbie observation PinmemberdbConner27-Oct-06 5:23 
I'm new to these forums.
 
Is it generally the protocol not to show the obvious error checking - for instance in the reference to user when assigning the datetime dt in the example?
 
I thought the code was useful.
GeneralRe: Newbie observation Pinmembercorvetteboy27-Oct-06 5:45 
GeneralRe: Newbie observation PinmemberMPatriot5-Nov-07 17:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 26 Oct 2006
Article Copyright 2006 by aggie_2000
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid