Repository navigation
Expand file tree
/
Copy pathAuthenticationConfiguration.cs
More file actions
109 lines (89 loc) · 4.21 KB
/
Copy pathAuthenticationConfiguration.cs
File metadata and controls
109 lines (89 loc) · 4.21 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Application.ViewModels.Users;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using API.Middlewares;
namespace API.Configurations
{
public static class AuthenticationConfiguration
{
public static void ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
var tokenKey = configuration["JwtToken:Key"];
if (string.IsNullOrEmpty(tokenKey))
throw new ArgumentNullException(nameof(tokenKey));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = configuration["JwtToken:Issuer"],
ValidAudience = configuration["JwtToken:Audience"],
IssuerSigningKey = new SymmetricSecurityKey
(Encoding.UTF8.GetBytes(tokenKey)),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
});
services.AddAuthorizationBuilder()
.AddPolicy("Moderator", policy => policy.RequireClaim("moderator", "True"));
}
public static void ConfigureApplicationAuthentication(this WebApplication app)
{
ArgumentNullException.ThrowIfNull(app);
app.UseAuthentication();
app.UseAuthorization();
}
public static string CreateAuthenticationToken(IConfiguration configuration, MyUserViewModel userViewModel)
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentNullException.ThrowIfNull(userViewModel);
var tokenKey = configuration["JwtToken:Key"];
if (string.IsNullOrEmpty(tokenKey))
throw new ArgumentNullException(nameof(tokenKey));
var issuer = configuration["JwtToken:Issuer"];
var audience = configuration["JwtToken:Audience"];
var key = Encoding.ASCII.GetBytes(tokenKey);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(
[
new Claim("Id", Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Sub, userViewModel.Id.ToString()),
new Claim(JwtRegisteredClaimNames.Email, userViewModel.Email!),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim("moderator", userViewModel.IsModerator.ToString())
]),
Expires = DateTime.UtcNow.AddDays(365),
Issuer = issuer,
Audience = audience,
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
public static string CreateAuthenticationToken(this MyUserViewModel userViewModel, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(userViewModel);
ArgumentNullException.ThrowIfNull(configuration);
return CreateAuthenticationToken(configuration, userViewModel);
}
public static void UseSetOnlineMiddleware(this WebApplication app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
app.UseMiddleware<SetOnlineMiddleware>();
}
}
}