微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 尽管设置了FormsAuthenticationTicket的UserData属性为空

出于某种原因,我的身份验证cookie的UserData属性为空.这是代码
var authCookie = FormsAuthentication.GetAuthCookie(userName,rememberUser.Checked);
// Get the FormsAuthenticationTicket out of the encrypted cookie
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
// Create a new FormsAuthenticationTicket that includes our custom User Data
var newTicket = new FormsAuthenticationTicket(ticket.Version,ticket.Name,ticket.IssueDate,ticket.Expiration,ticket.IsPersistent,"userData");
// Update the authCookie's Value to use the encrypted version of newTicket
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
// Manually add the authCookie to the Cookies collection
Response.Cookies.Add(authCookie);
FormsAuthentication.RedirectFromLoginPage(userName,rememberUser.Checked);

以下是我尝试访问它的方法

if (HttpContext.Current.Request.IsAuthenticated )
{
    var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie != null)
    {
        var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        string data = authTicket.UserData;
        // data is empty !!!
    }
}

解决方法

RedictFromLoginPage会覆盖您的cookie.删除此行并手动重定向(Response.Redirect).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐