Skip to content

Instantly share code, notes, and snippets.

@shawnmclean
Created January 12, 2012 19:48
Show Gist options
  • Save shawnmclean/1602683 to your computer and use it in GitHub Desktop.
Save shawnmclean/1602683 to your computer and use it in GitHub Desktop.
FormsAuthentication custom user data
public class FormsAuthenticationService : IFormsAuthenticationService
{
public void SignIn(UserIdentity user, bool createPersistentCookie)
{
//UserData is stored as json
string userData = JsonSerializer.SerializeToString<UserIdentity>(user);
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, //version
user.UserId.ToString(), // user name
DateTime.Now, //creation
DateTime.Now.AddMinutes(30), //Expiration
createPersistentCookie, userData); //storing the json data
string encTicket = FormsAuthentication.Encrypt(authTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
{
Expires = authTicket.Expiration,
Path = FormsAuthentication.FormsCookiePath
};
if (HttpContext.Current != null)
{
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
public void SignOut()
{
FormsAuthentication.SignOut();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment