Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Update JwtBearer sample error handling #1613
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jan 26, 2018
1 parent ab8328a commit 272aa16
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions samples/JwtBearerSample/Startup.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.ExceptionServices;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -43,33 +44,13 @@ public Startup(IHostingEnvironment env)
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// This can be removed after https://github.com/aspnet/IISIntegration/issues/371
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
// You also need to update /wwwroot/app/scripts/app.js
o.Authority = Configuration["jwt:authority"];
o.Audience = Configuration["jwt:audience"];
o.Events = new JwtBearerEvents()
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
OnAuthenticationFailed = c =>
{
c.NoResult();
c.Response.StatusCode = 500;
c.Response.ContentType = "text/plain";
if (Environment.IsDevelopment())
{
// Debug only, in production do not share exceptions with the remote host.
return c.Response.WriteAsync(c.Exception.ToString());
}
return c.Response.WriteAsync("An error occurred processing your authentication.");
}
};
});
// You also need to update /wwwroot/app/scripts/app.js
o.Authority = Configuration["oidc:authority"];
o.Audience = Configuration["oidc:clientid"];
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -86,13 +67,16 @@ public void Configure(IApplicationBuilder app)
app.Use(async (context, next) =>
{
// Use this if there are multiple authentication schemes
// var user = await context.Authentication.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
var user = context.User; // We can do this because of there's only a single authentication scheme
if (user?.Identity?.IsAuthenticated ?? false)
var authResult = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
if (authResult.Succeeded && authResult.Principal.Identity.IsAuthenticated)
{
await next();
}
else if (authResult.Failure != null)
{
// Rethrow, let the exception page handle it.
ExceptionDispatchInfo.Capture(authResult.Failure).Throw();
}
else
{
await context.ChallengeAsync();
Expand Down

0 comments on commit 272aa16

Please sign in to comment.