Skip to content

Instantly share code, notes, and snippets.

@EfrainReyes
Created November 28, 2014 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EfrainReyes/1b9f54c1d8089a45beb2 to your computer and use it in GitHub Desktop.
Save EfrainReyes/1b9f54c1d8089a45beb2 to your computer and use it in GitHub Desktop.
public class FormsAuthenticationService : IFormsAuthenticationService
{
private readonly HttpContextBase _context;
public FormsAuthenticationService(HttpContextBase context) {
_context = context;
}
public void SignIn(string userName, bool createPersistentCookie, IEnumerable<string> roles = null) {
string roleList = string.Empty;
if (roles != null)
roleList = string.Join(",", roles);
var authTicket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddDays(30),
createPersistentCookie,
roleList,
"/");
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
if (authTicket.IsPersistent) {
cookie.Expires = authTicket.Expiration;
}
_context.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