加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 22:16:31 所属栏目:asp.Net 来源:网络整理
导读:出于某种原因,我的身份验证cookie的UserData属性为空.这是代码: var authCookie = FormsAuthentication.GetAuthCookie(userName,rememberUser.Checked);// Get the FormsAuthenticationTicket out of the encrypted cookievar ticket = FormsAuthentication
出于某种原因,我的身份验证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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读