title: Request.IsAuthenticated when using Identity with Owin for MVC and WebAPI
body: <p>I have a ASP.Net WebAPI 2.1 that I have just transitioned across to using Identity 2.0 using bearer tokens.  This works fine.  Now I am attempting to pull in some MVC code to create a set of login and user management pages.  My issue is that I can't seem to get <code>Request.IsAuthenticated</code> to work from my Razor views when I set the WebApi <code>HttpConfiguration</code>to <code>SuppressDefaultHostAuthentication</code>.</p>
  
  <p>Below is my code, and I'm out of ideas as to how I can get this to work for both scenarios :(</p>
  
  <p>Here's my <code>Startup.cs</code> which sets up the Identity OWIN module, and the WebAPI:</p>
  
  <pre><code>public class Startup
  {
      public void Configure(IAppBuilder app)
      {
          app.CreatePerOwinContext(ApplicationDbContext.Create);
          app.CreatePerOwinContext&lt;ApplicationUserManager&gt;(ApplicationUserManager.Create);
          app.CreatePerOwinContext&lt;ApplicationRoleManager&gt;(ApplicationRoleManager.Create);
  
          PublicClientId = "self";
          OAuthOptions = new OAuthAuthorizationServerOptions
          {
              TokenEndpointPath = new PathString("/token"),
              Provider = new ApplicationOAuthProvider(PublicClientId),
              AuthorizeEndpointPath = new PathString("/api/account/externalLogin"),
              AccessTokenExpireTimeSpan = TimeSpan.FromDays(14)
          };
          app.UseOAuthBearerTokens(OAuthOptions);
  
          app.UseCookieAuthentication(new CookieAuthenticationOptions
          {
              AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
              Provider = new CookieAuthenticationProvider
              {
                  OnValidateIdentity = SecurityStampValidator.OnValidateIdentity&lt;ApplicationUserManager, ApplicationUser&gt;(
                      validateInterval: TimeSpan.FromMinutes(30),
                      regenerateIdentity: (manager, user) =&gt; user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie))
              }
          });
  
          var httpConfiguration = new HttpConfiguration();
          // Disable this line to allow Request.IsAuthenticated to work
          // But by doing this, it allows the 'redirect' to kick in on unauthenticated API requests, which returns a HTML page for a webapi call, rather than the JSON 'unauthenticated' response
          httpConfiguration.SuppressDefaultHostAuthentication();
          httpConfiguration.Filters.Add(new HostAuthenticationFilter(DefaultAuthenticationTypes.ApplicationCookie));
          httpConfiguration.MapHttpAttributeRoutes();
          app.UseWebApi(httpConfiguration);
      }
  }
  </code></pre>
  
  <p>Here's my <code>Global.asax.cs</code>, which sets up the MVC side of things (AFAIK OWIN doesn't support any form of <code>app.UseMvc()</code>):</p>
  
  <pre><code>public class WebApiApplication : HttpApplication
  {
      protected void Application_Start()
      {
          // pretty much the defaults here for everything, just renamed
          AreaRegistration.RegisterAllAreas();
          MvcConfig.ConfigureFilters(GlobalFilters.Filters);
          MvcConfig.ConfigureRoutes(RouteTable.Routes);
          MvcConfig.ConfigureBundles(BundleTable.Bundles);
      }
  }
  </code></pre>
  
  <p>Now in my Razor views, I would like to use <code>Request.IsAuthenticated</code>, as used in the Identity samples, but this fails when the <code>httpConfiguration.SuppressDefaultHostAuthentication</code> is enabled.  I understand the goal of this extension is to remove the current identity after the Identity middleware has run -- so that the WebAPI authentication filter can do as it pleases.  But I am hoping that on the MVC side of things, that the suppression wouldn't occur.</p>
  
  <p>Example Razor view:</p>
  
  <pre><code>@if (Request.IsAuthenticated) // false when using httpConfiguration.SuppressDefaultHostAuthentication
  {
    &lt;div&gt;User.Identity.Email&lt;/div&gt;
  }
  </code></pre>
  
  <p>Can anyone help me?  Is this even possible?</p>
  
  <p>Thanks!</p>