检查当前登录的用户是否具有持久性authcookie

Gre*_*reg 12 c# asp.net cookies persistence forms-authentication

我需要在当前登录用户的FormsAuthentication AuthCookie中编辑userdata.我不知道如何找出当前用户是否选择了持久性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);
Run Code Online (Sandbox Code Playgroud)

有人有任何想法吗?谢谢

Mar*_*n B 8

FormsAuthentication.GetAuthCookie方法仅创建一个新cookie.它不会让你获得早先制作的cookie.

在您的登录页面上,您可能有以下内容:

FormsAuthentication.GetAuthCookie (userID, chkPersistCookie.Checked)
Run Code Online (Sandbox Code Playgroud)

要知道用户何时通过身份验证,您可以执行此操作

this.Context.User.Identity.IsAuthenticated
Run Code Online (Sandbox Code Playgroud)

我实际上不确定您是否可以推断出用户具有持久身份验证cookie的事实.有一件事是检查cookie的有效期.

在这个问题中,有一个读取身份验证cookie的示例.


小智 7

这应该检索现有的表单auth cookie,检查票证,并判断它是否持久.

        var FormsAuthCookie = Response.Cookies[FormsAuthentication.FormsCookieName];
        var ExistingTicket = FormsAuthentication.Decrypt(FormsAuthCookie.Value);
        bool IsPersistent = ExistingTicket.IsPersistent;
Run Code Online (Sandbox Code Playgroud)