-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add dotnet user-jwts tool and runtime support #41520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
bf23154
6961df5
212cf04
3ae682e
5f5c040
2675c50
c37a2a6
212b42f
ef45270
751c1d7
6562366
672fb64
bd19796
1f3a990
23e9b0c
f0aa386
98b504f
50a3cda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Builder.WebApplication.Use(System.Func<Microsoft.AspNetCore.Http.RequestDelegate!, Microsoft.AspNetCore.Http.RequestDelegate!>! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder! | ||
Microsoft.AspNetCore.Builder.WebApplicationBuilder.Authentication.get -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder! | ||
static Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults(this Microsoft.Extensions.Hosting.IHostBuilder! builder, System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder!>! configure, System.Action<Microsoft.Extensions.Hosting.WebHostBuilderOptions!>! configureOptions) -> Microsoft.Extensions.Hosting.IHostBuilder! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Authentication; | ||
|
||
internal class WebApplicationAuthenticationBuilder : AuthenticationBuilder | ||
{ | ||
public bool IsAuthenticationConfigured { get; private set; } | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public WebApplicationAuthenticationBuilder(IServiceCollection services) : base(services) { } | ||
|
||
public override AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, Action<PolicySchemeOptions> configureOptions) | ||
{ | ||
RegisterServices(authenticationScheme); | ||
return base.AddPolicyScheme(authenticationScheme, displayName, configureOptions); | ||
} | ||
|
||
public override AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions) | ||
{ | ||
RegisterServices(authenticationScheme); | ||
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions); | ||
} | ||
|
||
public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions) | ||
{ | ||
RegisterServices(authenticationScheme); | ||
return base.AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions); | ||
} | ||
|
||
public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, Action<TOptions>? configureOptions) | ||
{ | ||
RegisterServices(authenticationScheme); | ||
return base.AddScheme<TOptions, THandler>(authenticationScheme, configureOptions); | ||
} | ||
|
||
private void RegisterServices(string authenticationScheme) | ||
{ | ||
if (!IsAuthenticationConfigured) | ||
{ | ||
IsAuthenticationConfigured = true; | ||
Services.AddAuthentication(authenticationScheme); | ||
Services.AddAuthorization(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
// The .NET Foundation licenses this file to you under the MIT license. | ||||||
|
||||||
using System.Diagnostics; | ||||||
using Microsoft.AspNetCore.Authentication; | ||||||
using Microsoft.AspNetCore.Hosting; | ||||||
using Microsoft.Extensions.Configuration; | ||||||
using Microsoft.Extensions.DependencyInjection; | ||||||
|
@@ -79,6 +80,7 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde | |||||
|
||||||
Host = new ConfigureHostBuilder(bootstrapHostBuilder.Context, Configuration, Services); | ||||||
WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services); | ||||||
Authentication = new WebApplicationAuthenticationBuilder(Services); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -113,6 +115,11 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde | |||||
/// </summary> | ||||||
public ConfigureHostBuilder Host { get; } | ||||||
|
||||||
/// <summary> | ||||||
/// An <see cref="AuthenticationBuilder"/> for configuration authentication-related properties. | ||||||
/// </summary> | ||||||
public AuthenticationBuilder Authentication { get; } | ||||||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
/// <summary> | ||||||
/// Builds the <see cref="WebApplication"/>. | ||||||
/// </summary> | ||||||
|
@@ -166,6 +173,12 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui | |||||
} | ||||||
} | ||||||
|
||||||
if (Authentication is WebApplicationAuthenticationBuilder webAuthBuilder && webAuthBuilder.IsAuthenticationConfigured is true) | ||||||
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.
Suggested change
|
||||||
{ | ||||||
app.UseAuthentication(); | ||||||
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. Do we need to guard this so that the middleware are only added if they're not already added in the app pipeline (like we do with the call to |
||||||
app.UseAuthorization(); | ||||||
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. Can we add something to 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. Yep, will add this to verify changes for #41520 (comment) |
||||||
} | ||||||
|
||||||
// Wire the source pipeline to run in the destination pipeline | ||||||
app.Use(next => | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.AspNetCore.Authentication; | ||
|
||
/// <summary> | ||
/// Provides an interface for implmenting a construct that provides | ||
/// access to specific configuration sections. | ||
/// </summary> | ||
public interface IAuthenticationConfigurationProvider | ||
{ | ||
/// <summary> | ||
/// Returns the specified <see cref="ConfigurationSection"/> object. | ||
/// </summary> | ||
/// <param name="authenticationScheme">The path to the section to be returned.</param> | ||
/// <returns>The specified <see cref="ConfigurationSection"/> object, or null if the requested section does not exist.</returns> | ||
IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Authentication.IAuthenticationConfigurationProvider | ||
Microsoft.AspNetCore.Authentication.IAuthenticationConfigurationProvider.GetAuthenticationSchemeConfiguration(string! authenticationScheme) -> Microsoft.Extensions.Configuration.IConfiguration! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.AspNetCore.Authentication; | ||
|
||
internal sealed class DefaultAuthenticationConfigurationProvider : IAuthenticationConfigurationProvider | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public DefaultAuthenticationConfigurationProvider(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme) | ||
{ | ||
return _configuration.GetSection($"Authentication:Schemes:{authenticationScheme}"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework> | ||
<UserSecretsId>MinimalJwtBearerSample-20151210102827</UserSecretsId> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.AspNetCore" /> | ||
<Reference Include="Microsoft.AspNetCore.Authentication" /> | ||
<Reference Include="Microsoft.AspNetCore.Authentication.JwtBearer" /> | ||
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Authentication.AddJwtBearer(); | ||
builder.Authentication.AddJwtBearer("ClaimedDetails"); | ||
|
||
builder.Services.AddAuthorization(options => | ||
options.AddPolicy("is_admin", policy => | ||
{ | ||
policy.RequireAuthenticatedUser(); | ||
policy.RequireClaim("is_admin", "true"); | ||
})); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGet("/protected", (ClaimsPrincipal user) => $"Hello {user.Identity?.Name}!") | ||
.RequireAuthorization(); | ||
|
||
app.MapGet("/protected-with-claims", (ClaimsPrincipal user) => | ||
{ | ||
return $"Glory be to the admin {user.Identity?.Name}!"; | ||
}) | ||
.RequireAuthorization("is_admin"); | ||
|
||
app.Run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:56852", | ||
"sslPort": 44385 | ||
} | ||
}, | ||
"profiles": { | ||
"MinimalJwtBearerSample": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "protected", | ||
"applicationUrl": "https://localhost:7259;http://localhost:5259", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "protected", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"Authentication": { | ||
DamianEdwards marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Schemes": { | ||
"Bearer": { | ||
"Audiences": [ | ||
"https://localhost:7259", | ||
"http://localhost:5259" | ||
], | ||
"ClaimsIssuer": "dotnet-user-jwts" | ||
}, | ||
"ClaimedDetails": { | ||
"Audiences": [ | ||
"https://localhost:7259", | ||
"http://localhost:5259" | ||
], | ||
"ClaimsIssuer": "dotnet-user-jwts" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.