System.Web.Security.FormsAuthentication.Encrypt 返回 null
我正在尝试加密一些 userData 以使用 Forms 身份验证创建我自己的自定义 IPrincipal 和 IIdentity 对象 - 我已将代表我登录用户的对象序列化为 Json 并创建了我的 FormsAuthentication 票证,如下所示:
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);
但是,字符串 encrypted_ticket
始终为null
。 user_item
字符串的长度有限制吗?
谢谢 穆斯塔法
I'm trying to encrypt some userData to create my own custom IPrincipal and IIdentity objects using Forms authentication - I've serialized an object representing my logged in user to Json and created my FormsAuthentication ticket like so:
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);
However, the string encrypted_ticket
is always null
. Is there a limit on the length of the user_item
string?
Thanks
Mustafa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为此问题的补充,当 userData 参数为
null
时,encrypted_ticket 也将为null
。在此示例中:
encrypted_ticket 现在生成
null
。但是,当对 userData 参数使用空字符串或string.Empty
时,我们会得到有效的 encrypted_ticket。MSDN 上也有一些记录
As an addition to this issue, when the userData parameter is
null
the encrypted_ticket will also benull
.In this example:
encrypted_ticket yields now
null
. However when using an empty string orstring.Empty
for the userData parameter we get a valid encrypted_ticket.This is also somewhat documented on MSDN
是的,典型的 cookie 限制是 ~4k。
添加加密后,您的数据将减少到 <2k。
你的代码是正确的..考虑:
产量:
虽然根据我的经验,臃肿的cookie会被截断而不是空,但你的问题可能是JSON包含会使你的cookie格式错误的字符,从而破坏它。
确保你的 json 大小合理,然后尝试
确保你测量了你的饼干,不要试图推动它,当你想在家看《迷失》并喝龙舌兰酒时,它会以微妙而恶毒的方式咬人。没有乐趣。
Yes, the typical cookie limit is ~4k.
Add encryption and you are down to <2k.
Your code is correct.. consider:
Yields:
While it is my experience that bloated cookies are truncated as opposed to nulled, your issue is probably that JSON contains characters that will make your cookie malformed, thus breaking it.
Make sure your json is a reasonable size, then try
Make sure you measure your cookies and don't try to push it, it will bite in subtle and vicious ways when you want to be home watching Lost and drinking tequila. no fun.
我使用此代码从登录页面重定向到可能的 deafault.aspx 页面,并且我的 UserData 为 Null,就像您的问题一样:
FormsAuthentication.RedirectFromLoginPage(username, false);
我更改代码,尝试此代码从 Login.aspx 重定向到 Default.aspx 页面,您的用户数据会很好:
i used this code to redirect from login page to may deafault.aspx page and my UserData was Null like your Problem:
FormsAuthentication.RedirectFromLoginPage(username, false);
i change the code , try this code to redirect from Login.aspx to Default.aspx page and your User Data will be fine: