| | 1 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 2 | | using Microsoft.Extensions.Configuration; |
| | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | 4 | | using Microsoft.IdentityModel.Tokens; |
| | 5 | | using System.Text; |
| | 6 | |
|
| | 7 | | namespace Infrastructure.Extensions |
| | 8 | | { |
| | 9 | | public static class AuthorizationExtensions |
| | 10 | | { |
| | 11 | | public const string UserRolePolicy = "user_role_policy"; |
| | 12 | | internal static void AddAuthentication(this IServiceCollection services, IConfiguration configuration) |
| 4 | 13 | | { |
| 4 | 14 | | services.AddAuthorization(); |
| 4 | 15 | | services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 4 | 16 | | .AddJwtBearer(o => |
| 1 | 17 | | { |
| 1 | 18 | | o.RequireHttpsMetadata = false; |
| 1 | 19 | | o.TokenValidationParameters = new TokenValidationParameters |
| 1 | 20 | | { |
| 1 | 21 | | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Authentication |
| 1 | 22 | | ValidIssuer = configuration["Authentication:Issuer"], |
| 1 | 23 | | ValidAudience = configuration["Authentication:Audience"], |
| 1 | 24 | | }; |
| 5 | 25 | | }); |
| | 26 | |
|
| 4 | 27 | | services.AddAuthorizationBuilder() |
| 5 | 28 | | .AddPolicy(UserRolePolicy, policy => policy.RequireRole("user")); |
| 4 | 29 | | } |
| | 30 | |
|
| | 31 | | } |
| | 32 | | } |