Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found the following article <a href="http://www.asp.net/web-api/overview/working-with-http/http-cookies" rel="nofollow">http://www.asp.net/web-api/overview/working-with-http/http-cookies</a> Using it I configured my AuthorizationHandler to use cookies:</p> <pre><code>public class AuthorizationHandler : DelegatingHandler { private readonly IAuthenticationService _authenticationService = new AuthenticationService(); protected override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var cookie = request.Headers.GetCookies(Constants.ApiKey).FirstOrDefault(); if (cookie != null) { var apiKey = cookie[Constants.ApiKey].Value; try { var guidKey = Guid.Parse(apiKey); var user = _authenticationService.GetUserByKey(guidKey); if (user != null) { var userIdClaim = new Claim(ClaimTypes.Name, apiKey); var identity = new ClaimsIdentity(new[] { userIdClaim }, "ApiKey"); var principal = new ClaimsPrincipal(identity); Thread.CurrentPrincipal = principal; } } catch (FormatException) { } } return base.SendAsync(request, cancellationToken); } } </code></pre> <p>I configured my Login action result:</p> <pre><code>[HttpPost] public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { var user = _authenticationService.Login(model); if (user != null) { _cookieHelper.SetCookie(user); return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "Incorrect username or password"); return View(model); } return View(model); } </code></pre> <p>Inside it I am using the CookieHelper, that I created. It consists of an interface:</p> <pre><code>public interface ICookieHelper { void SetCookie(User user); void RemoveCookie(); Guid GetUserId(); } </code></pre> <p>And a class that implements the interface:</p> <pre><code>public class CookieHelper : ICookieHelper { private readonly HttpContextBase _httpContext; public CookieHelper(HttpContextBase httpContext) { _httpContext = httpContext; } public void SetCookie(User user) { var cookie = new HttpCookie(Constants.ApiKey, user.UserId.ToString()) { Expires = DateTime.UtcNow.AddDays(1) }; _httpContext.Response.Cookies.Add(cookie); } public void RemoveCookie() { var cookie = _httpContext.Response.Cookies[Constants.ApiKey]; if (cookie != null) { cookie.Expires = DateTime.UtcNow.AddDays(-1); _httpContext.Response.Cookies.Add(cookie); } } public Guid GetUserId() { var cookie = _httpContext.Request.Cookies[Constants.ApiKey]; if (cookie != null &amp;&amp; cookie.Value != null) { return Guid.Parse(cookie.Value); } return Guid.Empty; } } </code></pre> <p>By having this configuration, now I can use the Authorize attribute for my ApiControllers:</p> <pre><code>[Authorize] public class TestController : ApiController { public string Get() { return String.Empty; } } </code></pre> <p>This means, that if the user is not logged in. He can not access my api and recieves a 401 error. Also I can retrieve the api key, which I use as a user ID, anywhere in my code, which makes it very clean and readable. <br /></p> <p>I do not think that using cookies is the best solution, as some user may have disabled them in their browser, but at the moment I have not found a better way to do the authorization.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload