Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 51 lines (44 sloc) 1.414 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
using System;
using System.Web;

namespace AppHarbor.Web.Security
{
public sealed class CookieAuthenticator : IAuthenticator
{
private readonly ICookieAuthenticationConfiguration _configuration;
private readonly HttpContextBase _context;

public CookieAuthenticator()
: this(new ConfigFileAuthenticationConfiguration(), new HttpContextWrapper(HttpContext.Current))
{
}

public CookieAuthenticator(ICookieAuthenticationConfiguration configuration, HttpContextBase context)
{
_configuration = configuration;
_context = context;
}

public void SetCookie(string username, bool persistent = false, string[] roles = null, byte[] tag = null)
{
var cookie = new AuthenticationCookie(0, Guid.NewGuid(), persistent, username, roles, tag);
using (var protector = new CookieProtector(_configuration))
{
var httpCookie = new HttpCookie(_configuration.CookieName, protector.Protect(cookie.Serialize()))
{
HttpOnly = true,
Secure = _configuration.RequireSSL,
};
if (persistent)
{
httpCookie.Expires = cookie.IssueDate + _configuration.Timeout;
}

_context.Response.Cookies.Add(httpCookie);
}
}

public void SignOut()
{
_context.Response.Cookies.Remove(_configuration.CookieName);
_context.Response.Cookies.Add(new HttpCookie(_configuration.CookieName, "")
{
Expires = DateTime.UtcNow.AddMonths(-100),
});
}
}
}
Something went wrong with that request. Please try again.