我正在尝试加密一些 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
的长度有限制吗字符串?谢谢
穆斯塔法
最佳答案
作为此问题的补充,当 userData 参数为 null
时encrypted_ticket 也将是 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);
encrypted_ticket 现在产生
null
.但是,当使用空字符串或 string.Empty
时对于 userData 参数,我们得到一个有效的 encrypted_ticket。这也在 MSDN 上有所记录。
Note
The userData parameter cannot be null.
关于asp.net - System.Web.Security.FormsAuthentication.Encrypt 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686335/