88 lines
4.0 KiB
C#
88 lines
4.0 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Modules.User.Application.Settings;
|
|
using Modules.User.Domain.Gateways;
|
|
using Modules.Media.Api;
|
|
using System.Text;
|
|
|
|
namespace Modules.User.Application
|
|
{
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddTokens(this IServiceCollection services, IConfigurationManager configuration, string jwtSettingsSection)
|
|
{
|
|
//Configuration
|
|
services.AddOptions<JwtSettings>().Bind(configuration.GetSection(jwtSettingsSection));
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
services.AddScoped<IPasswordHasher, PasswordHasher>();
|
|
services.AddScoped<IPasswordGateway, PasswordHasher>();
|
|
services.AddScoped<TokenGenerator>();
|
|
services.AddScoped<IRefreshTokenGateway, TokenGenerator>();
|
|
|
|
services.AddScoped<UserContext>();
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
|
|
{
|
|
options.RequireHttpsMetadata = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidIssuer = configuration.GetValue<string>("JwtSettings:Issuer"),
|
|
ValidateAudience = true,
|
|
ValidAudience = configuration.GetValue<string>("JwtSettings:Audience"),
|
|
ValidateLifetime = true,
|
|
ClockSkew = TimeSpan.Zero,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetValue<string>("JwtSettings:SecurityKey")!)),
|
|
ValidateIssuerSigningKey = true
|
|
};
|
|
});
|
|
services.AddAuthorization();
|
|
services.AddMediaStorage();
|
|
return services;
|
|
}
|
|
|
|
//public static IServiceCollection AddTokensCookie(this IServiceCollection services, IConfigurationManager configuration, string jwtSettingsSection)
|
|
//{
|
|
// //Configuration
|
|
// services.AddOptions<JwtSettings>().Bind(configuration.GetSection(jwtSettingsSection));
|
|
|
|
// services.AddHttpContextAccessor();
|
|
|
|
// services.AddScoped<IPasswordHasher, PasswordHasher>();
|
|
// services.AddScoped<IPasswordGateway, PasswordHasher>();
|
|
// services.AddScoped<TokenGenerator>();
|
|
// services.AddScoped<IRefreshTokenGateway, TokenGenerator>();
|
|
|
|
// services.AddScoped<UserContext>();
|
|
|
|
// services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
|
|
// {
|
|
// options.ClaimsIssuer = configuration.GetValue<string>("JwtSettings:Issuer");
|
|
// options.Cookie.Name = "auth_cookie";
|
|
// options.Cookie.HttpOnly = true;
|
|
// //options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
|
|
// options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
|
|
|
|
// //options.ke = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
|
|
// options.TokenValidationParameters = new TokenValidationParameters
|
|
// {
|
|
// ValidateIssuer = true,
|
|
// ValidateAudience = true,
|
|
// ValidAudience = configuration.GetValue<string>("JwtSettings:Audience"),
|
|
// ValidateLifetime = true,
|
|
// ClockSkew = TimeSpan.Zero,
|
|
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetValue<string>("JwtSettings:SecurityKey")!)),
|
|
// ValidateIssuerSigningKey = true
|
|
// };
|
|
// });
|
|
// services.AddAuthorization();
|
|
// services.AddMediaStorage();
|
|
// return services;
|
|
//}
|
|
}
|
|
}
|