title: How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity?
body: <p>Apologies and Thanks in advance for this question! I am still new to SO.</p>
  
  <p>I have been working on a web application using MVC5, EF6, and VS 2013. </p>
  
  <p>I spent some time upgrading to the RC bits once released. Thanks to all the great posts out there: eg. <a href="https://stackoverflow.com/questions/18727317/decoupling-microsoft-aspnet-identity?lq=1">Decoupling Microsoft.AspNet.Identity.*</a> and <a href="https://stackoverflow.com/questions/18416915/updating-asp-net-mvc-from-5-0-0-beta2-to-5-0-0-rc1/18426574#184265">Updating asp.net MVC from 5.0.0-beta2 to 5.0.0-rc1</a> !</p>
  
  <p>In my infinite wisdom, I decided to move to the RTM bits that @Hao Kung posted about here: <a href="https://stackoverflow.com/questions/18793746/how-can-i-get-early-access-to-upcoming-asp-net-identity-changes">How can I get early access to upcoming Asp.Net Identity changes?</a>. I figured I would save the trouble and not be too far behind when we finally receive the RTM build. </p>
  
  <p>This has either been a nightmare, or I am just completely missing something (or both) as I can not figure out basic tasks that had been working with the RC1 stuff. </p>
  
  <p>While it seems like I am logging the user in via the controller (<a href="https://stackoverflow.com/questions/18801120/where-is-microsoft-aspnet-identity-owin-authenticationmanager-in-asp-net-identit">Where is Microsoft.AspNet.Identity.Owin.AuthenticationManager in Asp.Net Identity RTM version?</a>) ... my WindowsIdentity is always empty and not authenticated after I call SignIn. The user and claimsIdentity object are correctly populated.</p>
  
  <p>Here is the action method I am calling (moved properties to local variables for completeness):</p>
  
  <pre><code>[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
  public virtual async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
  {
      if (ModelState.IsValid) {
          var userManager = new UserManager&lt;EtdsUser&gt;(new UserStore&lt;EtdsUser&gt;(new EtdsContext()));
          var user = userManager.Find(model.UserName, model.Password);
          if (user != null) {
              var authenticationManager = HttpContext.GetOwinContext().Authentication;
              authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer});
              var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
              authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity);
              return RedirectToLocal(returnUrl);
          }
      }
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
      return View(model);
  }
  </code></pre>
  
  <p>(On a side note: I do not need to log in external users at this time.)</p>
  
  <p>Any suggestions? 
  -or-
  Should I roll back all my changes and just wait until VS 2013 is RTMd?</p>
  
  <hr>
  
  <p>Update, refactored code to make it closer to @Hao Kung's original reply. However I still do not end up with a valid user identity. I think my AuthenticationManager is not assigned correctly?</p>
  
  <p>AuthenticationManger is now defined as:</p>
  
  <pre><code>public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
  </code></pre>
  
  <p>SignInAsync is now a separate method:</p>
  
  <pre><code>private async Task SignInAsync(EtdsUser user, bool isPersistent)
  {
      AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
      var claimsIdentity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
      AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, claimsIdentity);
  }
  </code></pre>
  
  <p>After "SignOut", the debugger shows:</p>
  
  <pre><code>AuthenticationManager.User.Identity
  {System.Security.Principal.WindowsIdentity}
      [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
      AuthenticationType: ""
      IsAuthenticated: false
      Name: ""
  </code></pre>
  
  <p>The "claimsIdentity" is then:</p>
  
  <pre><code>claimsIdentity
  {System.Security.Claims.ClaimsIdentity}
      Actor: null
      AuthenticationType: "ApplicationCookie"
      BootstrapContext: null
      Claims: {System.Security.Claims.ClaimsIdentity.get_Claims}
      IsAuthenticated: true
      Label: null
      Name: "alon"
      NameClaimType: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
      RoleClaimType: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
  </code></pre>
  
  <p>"SignIn" does not change anything:</p>
  
  <pre><code>AuthenticationManager.User.Identity
  {System.Security.Principal.WindowsIdentity}
      [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
      AuthenticationType: ""
      IsAuthenticated: false
      Name: ""
  </code></pre>
  
  <p>Still no Authentication, but seems that no errors are thrown. </p>
  
  <hr>
  
  <p>As answered by @Hao Kung, changed StartUp.Auth.cs from:</p>
  
  <pre><code>var authOptions = new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(4.0)};
  app.UseCookieAuthentication(authOptions);
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>var authOptions = new CookieAuthenticationOptions {
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
      LoginPath = new PathString("/Account/Login"),
      ExpireTimeSpan = TimeSpan.FromHours(4.0)
  }; ...
  </code></pre>