MyBookmark/Modules.User.Application/ServiceCollectionExtensions.cs
2024-12-29 20:16:52 +03:00

95 lines
4.4 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 System.Text;
using MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
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.TryAddEnumerable(ServiceDescriptor.Scoped<UserContext>().)
//services.TryAddEnumerable(ServiceDescriptor.Scoped<UserContext, UserContext>());
services.TryAddScoped<UserContext>();
services.AddHttpUserAgentMemoryCachedParser();
//services.AddHttpUserAgentParserAccessor(); // registers IHttpUserAgentParserAccessor, uses IHttpUserAgentParserProvider
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();
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;
//}
}
}