How to update last login date if “Remember Me” set?

旧城冷巷雨未停 提交于 2021-02-07 18:39:41

问题


When a user logs in to my site, the date of the visit is stamped in the database (User table). This is handled by the (custom) membership provider. However, if the user checks the "Remember me?" option when logging in, they are (naturally) not prompted to log in on subsequent visits. As the membership provider is not employed in this situation, the last login date is not updated in the database.

Using forms authentication, how can I ensure that the last login date is updated on each new visit to the site, rather just when they physically log in? Is there any event I can hook into to achieve this?

I can't use session state, as it is completely disabled in the web site I'm developing (the session module has been removed).

Thanks


回答1:


I'm assuming that you are using cookies (I can't see how the "Remember me" would work otherwise).

When a user logs-in, set two cookies, one permanent (if Remember Me is checked) and one temporary (this session-only). The second is what you use to authorize the user.

So, on a page where a user needs to be logged in, look for the session cookie. If found, continue as normal. If not found, look for the permanent cookie, if found, look the user in, set the log-in date, and set the session cookie. (If the permanent cookie isn't found, he just not logged in).




回答2:


Assuming you are talking about ASP.NET 2.0 (given the membership provider comment).

Wherever you are checking the cookie to see if the user should be autologged in, you should call out to the membership provider's GetUser function which takes a boolean to update the user's activity date.

Per the MSDN docs:

MembershipProvider.GetUser Method

Takes, as input, a unique user identifier and a Boolean value indicating whether to update the LastActivityDate value for the user to show that the user is currently online. The GetUser method returns a MembershipUser object populated with current values from the data source for the specified user. If the user name is not found in the data source, the GetUser method returns null (Nothing in Visual Basic).



来源:https://stackoverflow.com/questions/681929/how-to-update-last-login-date-if-remember-me-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!