-
Notifications
You must be signed in to change notification settings - Fork 593
Auth 2.0 Iteration 2 #1113
Changes from all commits
d86162b
2edb279
5b11d51
d193389
6f24590
bbb055f
5a99f5b
0732cc4
479085c
f3e9e70
a2a25f1
051a0ea
40b542f
7e256bc
bf607e1
84f11e8
0e89cea
9228486
4d7ed14
2429651
a9fe998
11564e2
b940578
d92d923
2aafea9
9a8d8ef
42ca776
8b8c549
06b32c6
b02da9e
7700e08
96c2c8b
a5d6d5a
d80b0d3
caf7ac7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
@@ -42,7 +43,28 @@ 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) | ||
{ | ||
services.AddAuthentication(); | ||
services.AddJwtBearerAuthentication(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() | ||
{ | ||
OnAuthenticationFailed = c => | ||
{ | ||
c.HandleResponse(); | ||
|
||
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."); | ||
} | ||
}; | ||
}); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
|
@@ -69,44 +91,22 @@ public void Configure(IApplicationBuilder app) | |
app.UseDefaultFiles(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseJwtBearerAuthentication(new JwtBearerOptions | ||
{ | ||
// You also need to update /wwwroot/app/scripts/app.js | ||
Authority = Configuration["jwt:authority"], | ||
Audience = Configuration["jwt:audience"], | ||
Events = new JwtBearerEvents() | ||
{ | ||
OnAuthenticationFailed = c => | ||
{ | ||
c.HandleResponse(); | ||
|
||
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."); | ||
} | ||
} | ||
}); | ||
app.UseAuthentication(); | ||
|
||
// [Authorize] would usually handle this | ||
app.Use(async (context, next) => | ||
{ | ||
// Use this if options.AutomaticAuthenticate = false | ||
// 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 options.AutomaticAuthenticate = true; | ||
var user = context.User; // We can do this because of there's only a single authentication scheme | ||
if (user?.Identity?.IsAuthenticated ?? false) | ||
{ | ||
await next(); | ||
} | ||
else | ||
{ | ||
// We can do this because of options.AutomaticChallenge = true; | ||
await context.Authentication.ChallengeAsync(); | ||
await context.ChallengeAsync(JwtBearerDefaults.AuthenticationScheme); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No more generic Challenge? It should work if there's only one or its the default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can preserve it, it would line up with naked Authorize with no DefaultPolicy then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well if we add an overload for Challenge that omits scheme, we should probably do the same for authenticate and SignIn, basically null scheme will mean use default (since the overloads are implemented as extension methods) |
||
} | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So AuthenticationManager is completely obsolete and you're rooting all of the extensions on HttpContext?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we should deprecate the Authentication versions as part of this