title: Using Owin authentication; not redirected to LoginPath
body: <p>Ran into a frustrating problem with ASP.NET Identity. The app does not redirect to the <code>LoginPath</code>, but instead serves a 401.</p>
  
  <p>I've added the <code>AuthorizeAttribute</code> filter to my filters and the <code>AllowAnonymous</code> attribute to the Login action.</p>
  
  <p>I have refactored <code>Configuration()</code> method in <code>Startup.cs</code> to:</p>
  
  <pre><code>public void Configuration(IAppBuilder app)
  {
      ConfigureAuth(app);
  }
  
  public void ConfigureAuth(IAppBuilder app)
  {
      app.UseCookieAuthentication(new CookieAuthenticationOptions
      {
          AuthenticationType = "ApplicationCookie",
          LoginPath = new PathString("/user/login")
      });
  }
  </code></pre>
  
  <p>as per this question:
  <a href="https://stackoverflow.com/questions/19647752/mvc-5-redirect-to-login-page-not-working-with-owin">MVC 5 Redirect to Login Page Not Working with OWIN</a></p>
  
  <p>I have edited the web.config file, and added the lines:</p>
  
  <pre><code>&lt;system.webServer&gt;
    &lt;modules&gt;
      &lt;remove name="FormsAuthentication" /&gt;
  &lt;/system.webServer&gt;
  </code></pre>
  
  <p>as per this question: <a href="https://stackoverflow.com/questions/21652808/asp-net-mvc-5-web-config-formsauthenticationmodule-or-formsauthentication/22772647#22772647">ASP.NET MVC 5 Web.config: &quot;FormsAuthenticationModule&quot; or &quot;FormsAuthentication&quot;</a></p>
  
  <p>I still don't get redirects. On the other side, I have no clue what these fine gentlemen are talking about: <a href="https://github.com/aspnet/Security/issues/607" rel="nofollow noreferrer">https://github.com/aspnet/Security/issues/607</a></p>
  
  <p>Apparently this line should be solving my problem:</p>
  
  <pre><code>app.UseCookieAuthentication(options =&gt; {
      options.AutomaticAuthenticate = true;
      options.AutomaticChallenge = true;
      options.LoginPath = "/Account/Login";
  });
  </code></pre>
  
  <p>The package that contains <code>UseCookieAuthentication</code> override that accepts lambdas or the <code>AutomaticAuthenticate</code> and <code>AutomaticChallenge</code> options is, I assume, <code>Microsof.AspNet.Authentication</code> and <code>Microsof.AspNet.Authentication.Cookies</code>, but both are missing from NuGet.</p>
  
  <p>The web.config contains the owin key: <code>&lt;add key="owin:AppStartup" value="SmartKG.App_Start.Startup" /&gt;</code></p>
  
  <p>EDIT: One peculiar thing I did not mention: Before I've added the <code>UserController</code>, which contains the Login action, I was getting redirected to the proper route, but was getting the missing resource error of course. Once I've added the controller and the action, problems with redirecting started to happen.</p>