Asp.net System.Web.Security.FormsAuthentication.Encrypt返回null
我正在尝试加密一些userData,以使用Forms身份验证创建我自己的自定义IPrincipal和IIdentity对象-我已将表示登录用户的对象序列化为Json,并创建了我的FormsAuthentication票证,如下所示:Asp.net System.Web.Security.FormsAuthentication.Encrypt返回null,asp.net,forms-authentication,Asp.net,Forms Authentication,我正在尝试加密一些userData,以使用Forms身份验证创建我自己的自定义IPrincipal和IIdentity对象-我已将表示登录用户的对象序列化为Json,并创建了我的FormsAuthentication票证,如下所示: string user_item = GetJsonOfLoggedinUser();/*get JSON representation of my logged in user*/ System.Web.Security.FormsAuthenticationT
string user_item = GetJsonOfLoggedinUser();/*get JSON representation of my logged in user*/
System.Web.Security.FormsAuthenticationTicket ticket =
new System.Web.Security.FormsAuthenticationTicket(1,
WAM.Utilities.SessionHelper.LoggedInEmployee.F_NAME + " "
+ WAM.Utilities.SessionHelper.LoggedInEmployee.L_NAME,
DateTime.Now, DateTime.Now.AddMinutes(30), false, user_item);
string encrypted_ticket = System.Web.Security.FormsAuthentication.Encrypt(ticket);
HttpCookie auth_cookie =
new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, encrypted_ticket);
Response.Cookies.Add(auth_cookie);
但是,字符串加密\u ticket
始终为null
。用户项目
字符串的长度是否有限制
谢谢
Mustafa是的,典型的cookie限制为~4k
添加加密,作为此问题的补充,当userData参数为
null
时,加密的\u票据也将为null
在本例中:
var ticket = new System.Web.Security.FormsAuthenticationTicket(1,
"username",
DateTime.Now, DateTime.Now.AddMinutes(30), false, null);
string encrypted_ticket = System.Web.Security.FormsAuthentication.Encrypt(ticket);
加密票据现在生成null
。但是,当对userData参数使用空字符串或string.empty
时,我们会得到一个有效的加密\u票证
这在某种程度上也有记录
注
userData参数不能为null
我使用此代码从登录页面重定向到may deafault.aspx页面,我的用户数据为空,就像您的问题一样: FormsAuthentication.RedirectFromLoginPage(用户名,false) 我更改了代码,尝试将此代码从Login.aspx重定向到Default.aspx页面,您的用户数据将正常:
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, false));
....
对我来说,如果票据上的Name属性为null,则Encrypt函数返回null。4KB限制适用于cookie,而不适用于加密值。如果加密值大于4KB,那么添加cookie的尝试将失败。我也有同样的问题,但我的@Mayo answer修复了itThanks@Mayo,你的技术解决了我的问题,Alhamdulillah(“所有的赞美都是真主一人的功劳”)。感谢提供详细信息-希望在userdata为null时引发异常。OMG谢谢。我花了2个小时试图解决这个问题,但一无所获。@Mustafakidd Right-这就像教科书中关于何时引发异常的示例。你是因为喝龙舌兰酒而迷路的吗?
string user_item = Server.UrlEncode(GetJsonOfLoggedinUser());
var ticket = new System.Web.Security.FormsAuthenticationTicket(1,
"username",
DateTime.Now, DateTime.Now.AddMinutes(30), false, null);
string encrypted_ticket = System.Web.Security.FormsAuthentication.Encrypt(ticket);
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, false));
....