c# - 将所有用户数据保存在 Forms 身份验证 cookie 中

标签 c# asp.net cookies forms-authentication httpcookie

我想存储电子邮件、column1 和 column2 以及用户类型。 它要求在每次页面加载时检查 column1 和 colmun2 值,我不想每次都从数据库中获取。

如何执行?

string type = User.Type;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, lg.UserName, DateTime.Now, DateTime.Now.AddMinutes(50), ckbRemember.Checked, User.Type,FormsAuthentication.FormsCookiePath);

最佳答案

您可以将最后一个参数设置为逗号分隔的字符串,如下面的代码所示。

FormsAuthenticationTicket ticket = null;
ticket = new FormsAuthenticationTicket(1, lg.UserName, DateTime.Now, 
        DateTime.Now.AddMinutes(50), ckbRemember.Checked,
        email + "," + column1 + ","+ column2 + "," + type);
//or just use line of code below after creating ticket object
//ticket.UserData = email + "," + column1 + ","+ column2 + "," + type;

然后,当你想读取forms authentication ticket的用户数据时,可以使用下面的代码。

FormsIdentity identity = (FormsIdentity)Context.User.Identity;
userData = identity.Ticket.UserData;
string[] data =  userData.Split(",".ToCharArray());
//get the data stored in UserData property of forms authentication ticket
string email =  data[0];
string column1 =  data[1];
string column2 = data[2];
string  userType = data[3];

关于c# - 将所有用户数据保存在 Forms 身份验证 cookie 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34870334/

相关文章:

android - 为 native 应用程序 webview 设置 cookie

c# - 在 nHibernate QueryOver<T> 期间无法比较字符串

javascript - ng-click 不会触发,功能不起作用。可以是范围吗?

c# - 单击链接按钮时显示面板

asp.net - 如何在 elmah 主题行中实现机器名称?

android - Android 应用程序中的 Cookie 同意?

c# - 我如何在 XNA 中找到旋转变换的 2D 坐标?

c# - 托管和非托管

c# - Windows Azure 表中的 Like 语句等效项

java - Android:BasicClientCookie/CookieStore 在 cookie 值周围添加引号