问题描述:

I want to store more information in the same cookie. Can I add values to this authentication cookie or do I have to use a second http cookie?

I want add FirstName, LastName, then return FirstName LastName

 public void SignIn(P_UserLogon_List_Result user, bool createPersistentCookie)

{

if (user == null)

throw new ArgumentNullException("user");

if (user.UserName == "administrator")

{

user.Role_name = "administrator";

var cookie = new LabCookie

{

UserName = user.UserName,

FirstName = user.Name,

LastName = user.Family,

RememberMe = false,

// Roles = new List<string> { "administrator" }

Roles = new List<string> { user.Role_name ?? "user" }

};

string userData = JsonConvert.SerializeObject(cookie);

var ticket = new FormsAuthenticationTicket(1, cookie.UserName, DateTime.Now,

DateTime.Now.Add(FormsAuthentication.Timeout),

createPersistentCookie, userData);

string encTicket = FormsAuthentication.Encrypt(ticket);

var httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) { Expires = DateTime.Now.Add(FormsAuthentication.Timeout) };

_httpContext.Response.Cookies.Add(httpCookie);

}

}

public class LabIdentity : IIdentity

{

public LabIdentity(FormsAuthenticationTicket ticket)

{

if (ticket == null)

{

Name = "Guest";

Roles = new List<string> { "guest" };

return;

}

var data = JsonConvert.DeserializeObject<LabCookie>(ticket.UserData);

if (data == null)

{

AsGuest();

return;

}

UserName = data.UserName;

FirstName = data.FirstName;

LastName = data.LastName;

Name = UserName;

//Email = data.Email;

Roles = data.Roles ?? new List<string> { "user" };

RememberMe = data.RememberMe;

}

private void AsGuest()

{

Name = "Guest";

Roles = new List<string> { "guest" };

}

public string UserName { get; set; }

//public string Email { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

//public TimeZoneInfo TimeZone { get; set; }

public bool RememberMe { get; set; }

public IList<string> Roles { get; set; }

#region IIdentity Members

public string AuthenticationType

{

get { return "labportal"; }

}

public bool IsAuthenticated

{

get { return !string.IsNullOrEmpty(UserName); }

}

public string Name { get; protected set; }

#endregion

}

相关阅读:
Top