从asp.net代码后面读取表单身份验证cookie

Tho*_*mas 34 asp.net

我们知道表单身份验证cookie已加密.那么如何从我后面的代码中读取表单身份验证cookie内容.

if (Request.Cookies[".ASPXAUTH"] != null)
{
    HttpCookie myCookie = new HttpCookie(".ASPXAUTH");
}
Run Code Online (Sandbox Code Playgroud)

Rya*_*anW 78

您可以使用FormsAuthentication提供的Decrypt方法访问该故障单

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

string cookiePath = ticket.CookiePath;
DateTime expiration = ticket.Expiration;
bool expired = ticket.Expired;
bool isPersistent = ticket.IsPersistent;
DateTime issueDate = ticket.IssueDate;
string name = ticket.Name;
string userData = ticket.UserData;
int version = ticket.Version;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你.另外,我相信最后一行,版本应该是int,而不是字符串.但我正在使用ASP.NET 4.5 .... (3认同)