尽管已设置,FormsAuthenticationTicket的UserData属性为空

时间:2022-09-06 20:29:56

For some reason, I the UserData property of my authentication cookie is empty. Here is the code:

出于某种原因,我的身份验证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);

Here is how I try to access it:

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

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 !!!
    }
}

3 个解决方案

#1


7  

RedictFromLoginPage overwrites your cookie. Remove this line and redirect manually (Response.Redirect).

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

#2


3  

This is a similar answer I answered few days ago.

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

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

You cannot use FormsAuthentication.SetAuthCookie or FormsAuthentication.RedirectFromLoginPage if you create FormsAuthenticationTicket by yourself.

如果您自己创建FormsAuthenticationTicket,则无法使用FormsAuthentication.SetAuthCookie或FormsAuthentication.RedirectFromLoginPage。

#3


0  

it seems to me you were doing the same tutorial... i have encountered the same problem in my case .. it was a web config error..

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

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

if you have cookieless="UseUri" in your web config remove it.. it work in my case

如果你的web配置中有cookieless =“UseUri”,请删除它..它适用于我的情况

#1


7  

RedictFromLoginPage overwrites your cookie. Remove this line and redirect manually (Response.Redirect).

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

#2


3  

This is a similar answer I answered few days ago.

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

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

You cannot use FormsAuthentication.SetAuthCookie or FormsAuthentication.RedirectFromLoginPage if you create FormsAuthenticationTicket by yourself.

如果您自己创建FormsAuthenticationTicket,则无法使用FormsAuthentication.SetAuthCookie或FormsAuthentication.RedirectFromLoginPage。

#3


0  

it seems to me you were doing the same tutorial... i have encountered the same problem in my case .. it was a web config error..

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

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

if you have cookieless="UseUri" in your web config remove it.. it work in my case

如果你的web配置中有cookieless =“UseUri”,请删除它..它适用于我的情况