Akash
Akash

Reputation: 125

Save All user data in Forms authentication cookies

I want to store email,column1 and column2 along with user-type. Its requirement to check column1 and and colmun2 values check in every page load, I dont want to fetch every-time from DataBase.

How do I perform it ?

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

Upvotes: 1

Views: 3233

Answers (1)

Sunil
Sunil

Reputation: 21406

You can set the last parameter as comma-delimited string as in code below.

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;

Then, when you want to read the user data of forms authentication ticket, you can use below code.

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];

Upvotes: 4

Related Questions