/
bazhinilya
/
net-simple-jwt
Обзор
Документация
Войти
/
bazhinilya
/
net-simple-jwt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
Extensions/ServiceCollectionExtensions.cs
66 строк
2 KB
bazhinilya
Initial commit
16 июн 2026, 23:19
16 июн 2026, 23:19
afe56b6
Код
Авторство
О чём код?
using net_simple_jwt.Controllers; using net_simple_jwt.Data; using net_simple_jwt.Events; using net_simple_jwt.Options; using net_simple_jwt.Services; namespace net_simple_jwt.Extensions; public static class ServiceCollectionExtensions { public static IServiceCollection AddMinimalAuth( this IServiceCollection services, Action<AuthOptions> configureOptions, Action<AuthEvents>? configureEvents = null) { var options = new AuthOptions(); configureOptions(options); // 1. Регистрируем события var events = new AuthEvents(); configureEvents?.Invoke(events); services.AddSingleton(events); // 2. Регистрируем DbContext (BLite) services.AddSingleton<AuthDbContext>(provider => new AuthDbContext(options.AuthDbConnectionString)); // 3. Регистрируем репозиторий и сервисы services.AddScoped<IUserRepository, UserRepository>(); services.AddScoped<IPasswordHasher, PasswordHasher>(); services.AddScoped<ITokenService>(provider => new TokenService( options.JwtKey, options.JwtIssuer, options.JwtAudience, options.AccessTokenExpiryMinutes ) ); // 4. Настраиваем JWT аутентификацию services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(opt => { opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = options.JwtIssuer, ValidateAudience = true, ValidAudience = options.JwtAudience, ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(options.JwtKey) ) }; }); services.AddAuthorization(); // 5. Регистрируем контроллеры из библиотеки services.AddControllers() .AddApplicationPart(typeof(AuthController).Assembly) .AddControllersAsServices(); return services; } }