views:

143

answers:

2

How do I set the LastLoginDate of the Membership user class to the UTC time instead of the server time?

The following code does not work even though LastLoginDate is a settable property.

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
    Dim user As MembershipUser = Membership.GetUser(Login1.UserName)
    If user IsNot Nothing Then
        If Membership.ValidateUser(Login1.UserName, Login1.Password) Then
            user.LastLoginDate = DateTime.UtcNow()
            e.Authenticated = True
        End If
    End If
End Sub

I also tried to do in Login1_LoggedIn and Login1_LoggingIn events and no luck.

+1  A: 

Perhaps you need to make a call to UpdateUser to save the change

http://msdn.microsoft.com/en-us/library/system.web.security.membership.updateuser.aspx

Membership.UpdateUser(u)
Sergio
+1  A: 

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

From the MSDN article on LastLoginDate:

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.

In your code:

If Membership.ValidateUser(Login1.UserName, Login1.Password) Then
    user.LastLoginDate = DateTime.UtcNow()
    MembershipUser.UpdateUser(user)
    e.Authenticated = True
End If
Oded
No luck again..I added the UpdateUser and tried all the 3 events (authenticate, loggedin and logging in)...Did not work...Am I overlooking something?
Soundar Rajan
I should have mentioned this eariler, but didn't think it was relevant - I am using the Microsoft Access membership provider. This provider code seems to have a bug, the line -> user.LastLoginDate = DateTime.UtcNow() does not work! So I ended up changing the source code to store only the UTC time.
Soundar Rajan