[英]Check if currently logged in user has persistent authcookie
I need to edit userdata in an a FormsAuthentication AuthCookie of the currently logged in user. 我需要在当前登录用户的FormsAuthentication AuthCookie中编辑userdata。 I don't see how to find out if the current user has chosen a persistent cookie ("Remember Me").
我不知道如何找出当前用户是否选择了持久性cookie(“记住我”)。
//user is already logged in...
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(username, ispersistant); //how to I determine 'ispersistant'?
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, NEWuserdata);
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
HttpContext.Current.Response.SetCookie(authCookie);
Anybody got any ideas? 有人有任何想法吗? Thanks
谢谢
This should retrieve the existing forms auth cookie, examine the ticket, and tell if it's persistent. 这应该检索现有的表单auth cookie,检查票证,并判断它是否持久。
var FormsAuthCookie = Response.Cookies[FormsAuthentication.FormsCookieName];
var ExistingTicket = FormsAuthentication.Decrypt(FormsAuthCookie.Value);
bool IsPersistent = ExistingTicket.IsPersistent;
The FormsAuthentication.GetAuthCookie
method only creates a new cookie. FormsAuthentication.GetAuthCookie
方法仅创建一个新cookie。 It does not get you the earlier made cookie. 它不会让你获得早先制作的cookie。
On your login page you probably have something like this: 在您的登录页面上,您可能有以下内容:
FormsAuthentication.GetAuthCookie (userID, chkPersistCookie.Checked)
And to know when the user is authenticated you can do 要知道用户何时通过身份验证,您可以执行此操作
this.Context.User.Identity.IsAuthenticated
I actually don't know for sure if you can deduce the fact that the user has a persistent auth cookie. 我实际上不确定您是否可以推断出用户具有持久身份验证cookie的事实。 One thing is checking the cookie for a expiry date.
有一件事是检查cookie的有效期。
In this question there is a example for reading the authentication cookie. 在这个问题中,有一个读取身份验证cookie的示例。
我最终在登录时在authcookie的userdata属性中存储了“ispersistant”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.