的FormsAuthenticationTicket的财产的UserData是,尽管被设置为空 [英] UserData property of FormsAuthenticationTicket is empty despite being set
本文介绍了的FormsAuthenticationTicket的财产的UserData是,尽管被设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某些原因,我在我的身份验证cookie的财产的UserData是空的。这里是code:
VAR authCookie = FormsAuthentication.GetAuthCookie(用户名,rememberUser.Checked);
//获取的FormsAuthenticationTicket了加密的cookie
VAR票= FormsAuthentication.Decrypt(authCookie.Value);
//创建一个新的FormsAuthenticationTicket,包括我们的自定义用户数据
VAR newTicket =新的FormsAuthenticationTicket(ticket.Version,ticket.Name,ticket.IssueDate,ticket.Expiration,ticket.IsPersistent,用户数据);
//更新authCookie的价值使用newTicket的加密版本
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
//手动authCookie添加到Cookies集合
Response.Cookies.Add(authCookie);
FormsAuthentication.RedirectFromLoginPage(用户名,rememberUser.Checked);
下面是我尝试访问它:
如果(HttpContext.Current.Request.IsAuthenticated)
{
VAR authCookie = HttpContext.Current.Request.Cookies [FormsAuthentication.FormsCookieName]
如果(authCookie!= NULL)
{
VAR authTicket = FormsAuthentication.Decrypt(authCookie.Value);
字符串数据= authTicket.UserData;
//数据是空的!
}
}
解决方案
RedictFromLoginPage
覆盖您的Cookie。删除此行并手动重定向(的Response.Redirect
)。
For some reason, I the UserData property of my authentication cookie is empty. Here is the code:
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 !!!
}
}
解决方案
RedictFromLoginPage
overwrites your cookie. Remove this line and redirect manually (Response.Redirect
).
这篇关于的FormsAuthenticationTicket的财产的UserData是,尽管被设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文