C# .NET - store last login date and time

Asked By Nitish Gupta on 29-Aug-11 10:35 PM
hello,
i am working in a forum project..i want a facilitate a user by giving his last login date and time ..in welcome page..when user login..
please tell me how can i implement above feature in my project..i am using asp.net C# and sql server..
please tell me full code and source also..
thanks in advance
Riley K replied to Nitish Gupta on 29-Aug-11 10:59 PM

You need to pass the modified user variable to MembershipUser.UpdateUser.

From the MSDN article on http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.lastlogindate.aspx:

You can also modify the last login date by setting the LastLoginDate property of a MembershipUser and passing the modified MembershipUser object to the UpdateUser method.

write this code

If Membership.ValidateUser(Login1.UserName, Login1.Password) Then
  user.LastLoginDate = DateTime.UtcNow()
  MembershipUser.UpdateUser(user)
  e.Authenticated = True
End If


Cheers
Anoop S replied to Nitish Gupta on 30-Aug-11 12:59 AM
Another approach is to, when logging in, read the last login date/time from the user record and save it into the session or a session cookie. Then update the user record with the current date/time. Then on your pages read the value stored in the session/cookie.

The old time will be removed when the session expires which is usually when a user needs to re-login anyway. It also has the advantage of speed and caching as it is reading from the session/cookie.

But it depends on your setup and app whether this is possible for you.

Just to be clear... The current date/time is persisted to the database user table every time the user logs in. But before the date/time is written to the user table, the existing value is read and saved to the session or cookie. You then update the date/time value in the user table with the current timestamp.

If your authentication ticket lasts longer than the session then use the cookie method and set the expiry of the cookie to the same expiry of the authentication ticket.
TSN ... replied to Nitish Gupta on 30-Aug-11 02:34 AM
hi..

if you are not using membership this would be your answer...

 Hello,

Add lastlogin column in you login table

Updated lastLogin column value as getdate() based on login userid when user login.

Get the LastLogin value  when user login

--This query returns the UserID and LastLogin that were created in last  5 minutes.

CREATE PROCEDURE [dbo].[CheckLastLogin]
(
@Date datetime OUTPUT
)
AS
BEGIN
 
DECLARE @compareDate datetime
 
SET @compareDate = DATEADD(minute,-5,GetDate())
 
  SELECT
    LastLogin,UserID
  FROM
    Login
  WHERE    
    LastLogin >= @compareDate
    
END

 call this  procedure from asp.net....

I hope this will help you
dipa ahuja replied to Nitish Gupta on 30-Aug-11 06:43 AM
Untitled document
protected void Button1_Click(object sender, EventArgs e)
{
 String conn = "<Your connectionSTring>";
 SqlConnection connection = new SqlConnection(conn);
 connection.Open();
 string q = "select * from users where  username='" + txtuname.Text + "' AND pwd ='" + txtpwd.Text + "'";
  SqlDataAdapter da = new SqlDataAdapter(q, connection);
 DataSet ds = new DataSet();
 da.Fill(ds);
  if (ds.Tables[0].Rows.Count == 0)
 {
   lblerror.Text = "Invalid Login or password!";
 }
 TimeSpan diff;
  DateTime dt = DateTime.Now;
 DateTime dt2 = DateTime.Parse(ds.Tables[0].Rows[0]["lastlogin"].ToString());
  //Get the difference
 diff = dt.Subtract(dt2);
  if (diff.Days > 61)
 {
   //User is not login from 2 months
   Response.Redirect("PasswordExpired.aspx");
 }
 else
 {
   Response.Redirect("Home.aspx");
 }
 connection.Close();
   
}

Hope this will help you!