【问题标题】:UserData property of FormsAuthenticationTicket is empty despite being setFormsAuthenticationTicket 的 UserData 属性为空,尽管已设置
【发布时间】:2013-05-11 18:53:43
【问题描述】:

由于某种原因,我的身份验证 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 !!!
    }
}

【问题讨论】:

    标签: asp.net cookies forms-authentication


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    这是我几天前回答的类似答案。

    https://stackoverflow.com/a/16365000/296861

    如果您自己创建 FormsAuthenticationTicket,则不能使用 FormsAuthentication.SetAuthCookieFormsAuthentication.RedirectFromLoginPage

    【讨论】:

    • 我意识到FormsAuthentication.GetRedirectUrl也有这个问题。检索 url 并将其存储到变量中,然后设置 cookie,最后进行重定向解决了问题。
    【解决方案3】:

    在我看来你正在做同样的教程...... 我遇到了同样的问题 在我的情况下..这是一个网络配置错误..

     <authentication mode="Forms">
        <forms slidingExpiration="true" timeout="60" cookieless="UseUri"/>
      </authentication>
    

    如果您的网络配置中有 cookieless="UseUri" ,请将其删除。 它适用于我的情况

    【讨论】:

      【解决方案4】:

      FormsAuthenticationTicket 可能以空的 UserData 值结束的另一个原因 - 这导致通过在身份验证票证上调用 FormsAuthentication.Encrypt 生成的加密身份验证令牌也为空 - 是如果 null 是指定为FormsAuthenticationTicket constructoruserData 参数的值。

      如果您没有要在身份验证票证上设置特殊用户数据,则使用空字符串作为参数,而不是 null

      也就是说,而不是:

      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
          myAuthenticationTicketVersion, 
          authenticatedUserName, 
          DateTime.Now, 
          myExpirationDate,
          true, 
          null); // The null value here causes the problem
      

      改为这样做:

      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
          myAuthenticationTicketVersion, 
          authenticatedUserName, 
          DateTime.Now, 
          myExpirationDate,
          true, 
          ""); // Ok
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 2023-01-02
        • 2019-07-03
        • 1970-01-01
        • 2017-07-14
        相关资源
        最近更新 更多