title: MVC 6 Cookie Authentication - Getting User Details From the Cookie
body: <p>I'm working on an MVC 6 application that does <strong>not</strong> use Entity or Identity.  Instead, I'm using Dapper.  I have a controller that accepts a POST request and uses Dapper to check the database to see if the users name / password match.  All I'd like to do is store the users name and whether they're logged in or not so I can make that check on subsequent pages.</p>
  
  <p>I looked around and it seems like using Cookie based authentication should allow me to do what I want.  Here's the relevant code in my <code>Startup.cs</code> file:</p>
  
  <pre class="lang-cs prettyprint-override"><code>app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
      LoginPath = "/account/login",
      AuthenticationScheme = "Cookies",
      AutomaticAuthenticate = true,
      AutomaticChallenge = true
  });
  </code></pre>
  
  <p>Here's what the relevant code in my controllers login action looks like:
  </p>
  
  <pre><code>var user = _repo.FindByLogin(model.VendorId, model.Password);
  if (user != null) {
   var claims = new List &lt; Claim &gt; {
    new Claim("VendorId", user.VendorId),
    new Claim("Name", "john")
   };
  
   var id = new ClaimsIdentity(claims, "local", "name", "role");
   await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(id));
   var l = ClaimsIdentity.DefaultNameClaimType;
   return RedirectToAction("Index", "PurchaseOrders");
  }
  </code></pre>
  
  <p>The above code <em>seems</em> to work in that a cookie is being saved, but I'm not sure how I would go about getting the user information back out of the cookie (or even how to retrieve the cookie on subsequent requests in the first place).</p>
  
  <p>In my mind I'm imagining being able to do something like: <code>var user = (User)HttpContext.Request.Cookies.Get(????)</code>, but I'm not sure if that's practical or not.</p>