asp.net - System.Web.Security.FormsAuthentication.Encrypt 返回 null

标签 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.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/

相关文章:

c# - 在 asp.net C# 中重定向按钮单击时的新选项卡或浏览器。(Response.Redirect)

c# - 在 3 个网站之间共享身份验证

c# - 登录状态不显示已登录

javascript - 解析.com : current user not available in other page after login

asp.net - 每个 CPU 或每个内存的 ASP.NET session 的典型平均数量是多少?

javascript - 如何在asp.net c#中将asp数据表绑定(bind)到字符串数组?

javascript - 单击按钮时使用 JQuery 显示/隐藏某些 Div

Azure session 表在 AspProvider 的 TableStorageSessionStateProvider 中保持为空

c# - 应用程序在闲置 10 分钟后注销用户

c# - 如何将数据绑定(bind)到 Kentico CMS 中的查询转发器?