Skip to content

Commit 4835304

Browse files
committed
Add Authorization extension method
1 parent 4719fea commit 4835304

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace Microsoft.AspNetCore.Builder;
8+
9+
/// <summary>
10+
/// Extension methods for setting up authorization services in an <see cref="WebApplicationBuilder" />.
11+
/// </summary>
12+
public static class AuthorizationWebApplicationBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Registers services required by authentication services.
16+
/// </summary>
17+
/// <param name="builder">The <see cref="WebApplicationBuilder"/>.</param>
18+
/// <returns>A <see cref="AuthorizationBuilder"/> that can be used to further configure authentication.</returns>
19+
public static AuthorizationBuilder AddAuthorization(this WebApplicationBuilder builder)
20+
{
21+
if (builder == null)
22+
{
23+
throw new ArgumentNullException(nameof(builder));
24+
}
25+
26+
return builder.Services.AddAuthorizationBuilder();
27+
}
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#nullable enable
22
Microsoft.AspNetCore.Builder.AuthenticationWebApplicationBuilderExtensions
3+
Microsoft.AspNetCore.Builder.AuthorizationWebApplicationBuilderExtensions
34
Microsoft.AspNetCore.Builder.WebApplication.Use(System.Func<Microsoft.AspNetCore.Http.RequestDelegate!, Microsoft.AspNetCore.Http.RequestDelegate!>! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
45
static Microsoft.AspNetCore.Builder.AuthenticationWebApplicationBuilderExtensions.AddAuthentication(this Microsoft.AspNetCore.Builder.WebApplicationBuilder! builder) -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder!
56
static Microsoft.AspNetCore.Builder.AuthenticationWebApplicationBuilderExtensions.AddAuthentication(this Microsoft.AspNetCore.Builder.WebApplicationBuilder! builder, string! defaultScheme) -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder!
67
static Microsoft.AspNetCore.Builder.AuthenticationWebApplicationBuilderExtensions.AddAuthentication(this Microsoft.AspNetCore.Builder.WebApplicationBuilder! builder, System.Action<Microsoft.AspNetCore.Authentication.AuthenticationOptions!>! configureOptions) -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder!
8+
static Microsoft.AspNetCore.Builder.AuthorizationWebApplicationBuilderExtensions.AddAuthorization(this Microsoft.AspNetCore.Builder.WebApplicationBuilder! builder) -> Microsoft.AspNetCore.Authorization.AuthorizationBuilder!
79
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!

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,11 +1990,10 @@ public async Task RegisterAuthMiddlewaresCorrectly()
19901990
var username = "foobar";
19911991

19921992
var builder = WebApplication.CreateBuilder();
1993-
builder.Services.AddAuthenticationCore(o =>
1993+
builder.AddAuthentication(o =>
19941994
{
19951995
o.DefaultScheme = "testSchemeName";
1996-
});
1997-
builder.AddAuthentication().AddScheme<AuthenticationSchemeOptions, UberHandler>("testSchemeName", "testDisplayName", _ => { });
1996+
}).AddScheme<AuthenticationSchemeOptions, UberHandler>("testSchemeName", "testDisplayName", _ => { });
19981997
builder.WebHost.UseTestServer();
19991998
await using var app = builder.Build();
20001999

src/Security/Authentication/JwtBearer/samples/MinimalJwtBearerSample/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
.AddJwtBearer("ClaimedDetails")
1414
.AddJwtBearer("InvalidScheme");
1515

16-
builder.Services.AddAuthorization(options =>
17-
options.AddPolicy("is_admin", policy =>
16+
builder.AddAuthorization()
17+
.AddPolicy("is_admin", policy =>
1818
{
1919
policy.RequireAuthenticatedUser();
2020
policy.RequireClaim("is_admin", "true");
21-
}));
21+
});
2222

2323
var app = builder.Build();
2424

0 commit comments

Comments
 (0)