Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 51 lines (44 sloc) 1.414 kb
aaffb24 @mehrdada Initial commit
mehrdada authored
1 using System;
2 using System.Web;
3
4 namespace AppHarbor.Web.Security
5 {
6 public sealed class CookieAuthenticator : IAuthenticator
7 {
8 private readonly ICookieAuthenticationConfiguration _configuration;
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
9 private readonly HttpContextBase _context;
aaffb24 @mehrdada Initial commit
mehrdada authored
10
11 public CookieAuthenticator()
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
12 : this(new ConfigFileAuthenticationConfiguration(), new HttpContextWrapper(HttpContext.Current))
aaffb24 @mehrdada Initial commit
mehrdada authored
13 {
14 }
15
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
16 public CookieAuthenticator(ICookieAuthenticationConfiguration configuration, HttpContextBase context)
aaffb24 @mehrdada Initial commit
mehrdada authored
17 {
18 _configuration = configuration;
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
19 _context = context;
aaffb24 @mehrdada Initial commit
mehrdada authored
20 }
21
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
22 public void SetCookie(string username, bool persistent = false, string[] roles = null, byte[] tag = null)
aaffb24 @mehrdada Initial commit
mehrdada authored
23 {
12e0724 @mehrdada Added version outside cookie and roles.
mehrdada authored
24 var cookie = new AuthenticationCookie(0, Guid.NewGuid(), persistent, username, roles, tag);
aaffb24 @mehrdada Initial commit
mehrdada authored
25 using (var protector = new CookieProtector(_configuration))
26 {
27 var httpCookie = new HttpCookie(_configuration.CookieName, protector.Protect(cookie.Serialize()))
28 {
29 HttpOnly = true,
30 Secure = _configuration.RequireSSL,
31 };
93ca90d @mehrdada Fixed the persistent cookie code
mehrdada authored
32 if (persistent)
aaffb24 @mehrdada Initial commit
mehrdada authored
33 {
34 httpCookie.Expires = cookie.IssueDate + _configuration.Timeout;
35 }
36
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
37 _context.Response.Cookies.Add(httpCookie);
aaffb24 @mehrdada Initial commit
mehrdada authored
38 }
39 }
40
41 public void SignOut()
42 {
fdc18b1 @mehrdada Made the design more testable by taking a HttpContextBase from
mehrdada authored
43 _context.Response.Cookies.Remove(_configuration.CookieName);
44 _context.Response.Cookies.Add(new HttpCookie(_configuration.CookieName, "")
45 {
46 Expires = DateTime.UtcNow.AddMonths(-100),
47 });
aaffb24 @mehrdada Initial commit
mehrdada authored
48 }
49 }
50 }
Something went wrong with that request. Please try again.