/
fr1n1ss
/
PasswordManager
Обзор
Документация
Войти
/
fr1n1ss
/
PasswordManager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
PasswordManagerAPI/Program.cs
147 строк
6 KB
Dmitry Kiselyov
docker setup
31 май 2026, 21:46
31 май 2026, 21:46
ab7324b
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using PasswordManagerAPI.Entities; using PasswordManagerAPI.Services; using System.IdentityModel.Tokens.Jwt; using System.Text; namespace PasswordManagerAPI { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString)); var key = Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"] ?? "default_key"); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = true; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = true, ValidateAudience = true, ValidIssuer = builder.Configuration["Jwt:Issuer"], ValidAudience = builder.Configuration["Jwt:Audience"], ClockSkew = TimeSpan.Zero }; options.Events = new JwtBearerEvents { OnTokenValidated = async context => { var db = context.HttpContext.RequestServices.GetRequiredService<AppDbContext>(); var userIdRaw = context.Principal?.FindFirst("userId")?.Value; var jwtId = context.Principal?.FindFirst(JwtRegisteredClaimNames.Jti)?.Value; if (!int.TryParse(userIdRaw, out var userId) || string.IsNullOrWhiteSpace(jwtId)) { context.Fail("Invalid session claims"); return; } var session = await db.UserSessions.FirstOrDefaultAsync(x => x.UserId == userId && x.JwtId == jwtId); if (session == null || session.RevokedAt != null || session.ExpiresAt <= DateTime.UtcNow) { context.Fail("Session is no longer active"); return; } session.LastSeenAt = DateTime.UtcNow; await db.SaveChangesAsync(); } }; }); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); builder.Services.AddControllers(); builder.Services.AddHttpContextAccessor(); builder.Services.AddScoped<IAccountService, AccountService>(); builder.Services.AddScoped<INoteService, NoteService>(); builder.Services.AddScoped<IFavoriteService, FavoriteService>(); builder.Services.AddScoped<ITotpService,TotpService>(); builder.Services.AddScoped<IQrReaderService, QrReaderService>(); builder.Services.AddScoped<IAuditService, AuditService>(); builder.Services.AddScoped<IEmailSender, SmtpEmailSender>(); builder.Services.AddScoped<IEmailVerificationService, EmailVerificationService>(); builder.Services.AddScoped<PasswordPolicyService>(); builder.Services.AddEndpointsApiExplorer(); builder.WebHost.UseKestrel(options => { options.ListenAnyIP(8080); }); builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "Password Manager API", Version = "v1" }); options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.Http, Scheme = "Bearer", BearerFormat = "JWT", In = ParameterLocation.Header }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] {} } }); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseCors("AllowAll"); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); if (builder.Configuration.GetValue<bool>("Database:ApplyMigrationsOnStartup")) { using var scope = app.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); db.Database.Migrate(); } app.Run(); } } }