-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthorizationExtensions.cs
44 lines (39 loc) · 1.6 KB
/
AuthorizationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Audis.OpenID.Authorization.Configuration;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Audis.OpenID.Authorization
{
public static class AuthorizationExtensions
{
public static void AddResourceAuthorization(this IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var authorizationSettings = configuration.GetAuthorizationSettings();
services.AddResourceAuthorization(authorizationSettings);
}
public static void AddResourceAuthorization(this IServiceCollection services, AuthorizationSettings authorizationSettings)
{
var (issuer, audience) = authorizationSettings;
services
.AddAuthorization()
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = issuer;
options.Audience = audience;
});
}
public static void UseResourceAuthorization(this IApplicationBuilder app)
{
app.UseAuthentication();
app.UseAuthorization();
}
}
}