forked from ucosmic/UCosmicAlpha
/
IdentityController.cs
67 lines (57 loc) · 2.04 KB
/
IdentityController.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Security;
using AttributeRouting;
using AttributeRouting.Web.Http;
using UCosmic.Web.Mvc.Models;
namespace UCosmic.Web.Mvc.ApiControllers
{
[RoutePrefix("api")]
public class IdentityController : ApiController
{
private readonly ISignUsers _userSigner;
private readonly IStorePasswords _passwords;
public IdentityController(ISignUsers userSigner
, IStorePasswords passwords
)
{
_userSigner = userSigner;
_passwords = passwords;
}
[POST("sign-in")]
public HttpResponseMessage SignIn(SignInApiModel model)
{
if (!_passwords.Validate(model.UserName, model.Password))
{
var badRequest = Request.CreateResponse(HttpStatusCode.BadRequest);
return badRequest;
}
var authCookie = FormsAuthentication.GetAuthCookie(model.UserName,
true /* web api cookie must be persistent to appear in document.cookies */);
var cookieHeader = new CookieHeaderValue(FormsAuthentication.FormsCookieName, authCookie.Value)
{
Expires = authCookie.Expires,
Domain = authCookie.Domain,
Path = authCookie.Path,
};
var response = new HttpResponseMessage();
response.Headers.AddCookies(new[] { cookieHeader });
return response;
}
[GET("sign-out")]
public HttpResponseMessage GetSignOut()
{
var resp = new HttpResponseMessage();
var cookie = new CookieHeaderValue(FormsAuthentication.FormsCookieName, "NoCookie")
{
Expires = new DateTime(1999, 10, 12),
};
resp.Headers.AddCookies(new[] { cookie });
_userSigner.SignOff();
return resp;
}
}
}