How do I set the LastLoginDate of the ASP.NET MembershipUser?
H开发者_JAVA百科ow 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.
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)
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
精彩评论