title: ASP .NET Core 2.0 - JWT External Auth
body: <p>I am trying to get started with authentication on an ASP.NET Core 2.0 web app.</p>
  
  <p>My company is using Ping Federate and I am trying to authenticate my users using the company login page and in return validating the returned token using my signing key (<code>X509SecurityKey</code> down here).</p>
  
  <p>The login page link looks like:</p>
  
  <p><code>https://companyname.com/authorization.oauth2?response_type=code&amp;redirect_uri=https%3a%2f%2fJWTAuthExample%2fAccount%2fLogin&amp;client_id=CompanyName.Web.JWTAuthExample&amp;scope=&amp;state=&lt;...state...&gt;</code></p>
  
  <p>Out of the box, I configured the Startup.cs to be able to log in and challenge against this site.</p>
  
  <p>I decorated my HomeController with a <code>[Authorize(Policy="Mvc")]</code> but when I access one of the pages, I just get a blank page.</p>
  
  <p>Debug is not hitting the <code>OnChallenge</code> or <code>OnAuthenticationFailed</code> methods when I add it to <code>options.Events</code> (I think because user needs to be authenticated first).</p>
  
  <p>So, what am I missing in order for a redirect to my authentication website to happen? Is it built in or do I have to do some manual configuration?</p>
  
  <p>(Note: In other web apps, using asp net framework, I use a redirect in an Authorize attribute when authentication fails)</p>
  
  <p>Related post: <a href="https://stackoverflow.com/questions/46022861/authorize-attribute-does-not-redirect-to-login-page-when-using-net-core-2s-add">Authorize attribute does not redirect to Login page when using .NET Core 2&#39;s AddJwtBearer</a>
  - From this post, does it mean I am not using the right authentication method? I am building a web app, not an API.</p>
  
  <pre><code>namespace JWTAuthExample
  {
      public class Startup
      {
          public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
          {
              Configuration = configuration;
              HostingEnvironment = hostingEnvironment;
  
              string certificatepath = Path.Combine(HostingEnvironment.ContentRootPath, $"App_Data\\key.cer");
              KEY = new X509SecurityKey(new X509Certificate2(certificatepath));
          }
  
          public IConfiguration Configuration { get; }
          public IHostingEnvironment HostingEnvironment { get; }
          private string AUTH_LOGINPATH { get; } = Configuration["DefaultAuth:AuthorizationEndpoint"];
          private X509SecurityKey KEY { get; }
  
  
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddMvc();
  
              services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =&gt;
                  {
                      options.IncludeErrorDetails = true;
                      options.SaveToken = true;
                      options.TokenValidationParameters = new TokenValidationParameters
                      {   
                          // Ensure token expiry
                          RequireExpirationTime = true,
                          ValidateLifetime = true,
                          // Ensure token audience matches site audience value
                          ValidateAudience = false,
                          ValidAudience = AUTH_LOGINPATH,
                          // Ensure token was issued by a trusted authorization server
                          ValidateIssuer = true,
                          ValidIssuer = AUTH_LOGINPATH,
                          // Specify key used by token
                          RequireSignedTokens = true,
                          IssuerSigningKey = KEY
                      };
                  });
  
              services.AddAuthorization(options =&gt;
              {
                  options.AddPolicy("Mvc", policy =&gt;
                  {
                      policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
                      policy.RequireAuthenticatedUser();                    
                  });
              });
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseBrowserLink();
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Home/Error");
              }
  
              app.UseStaticFiles();
  
              app.UseAuthentication();
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: "default",
                      template: "{controller=Home}/{action=Index}/{id?}");
              });
          }
      }
  }
  </code></pre>