Created
January 12, 2012 19:48
-
-
Save shawnmclean/1602683 to your computer and use it in GitHub Desktop.
FormsAuthentication custom user data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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