Getting the user Login date and time when using FormsAuthentication


When you want to get the user login date/time  you can follow one of the following ways :

Through the user Ticket:

Get the current ticket of the logged in user and through it’s IssueDate property , you will get what you are looking for :

if (HttpContext.Current.User.Identity is FormsIdentity)
            // get the identity of the user
            FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
            // if authenticated , 
            //get the login date/time which is exactly the same as the authentication ticket issue date.
            if (identity.IsAuthenticated)
                DateTime loginDate = identity.Ticket.IssueDate;

Through the membership Provider :

If you are using the Membership provider, you can get that user login date as follows:

 if (Request.IsAuthenticated)
            MembershipUser muser = Membership.GetUser();
            DateTime LoginDate = muser.LastLoginDate;

No Comments