From 4a4ee24ce7685b65f29d69e786a61893ca4b845b Mon Sep 17 00:00:00 2001 From: kittyegg Date: Tue, 16 Jun 2026 00:59:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ShortUrlController.cs | 4 ++++ URL-Shortener/Program.cs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/URL-Shortener/Controllers/ShortUrlController.cs b/URL-Shortener/Controllers/ShortUrlController.cs index 876a657..bcc62a6 100644 --- a/URL-Shortener/Controllers/ShortUrlController.cs +++ b/URL-Shortener/Controllers/ShortUrlController.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using URL_Shortener.DTO; using URL_Shortener.Models; using HashidsNet; +using Microsoft.AspNetCore.Authorization; namespace URL_Shortener.Controllers { @@ -78,12 +79,15 @@ namespace URL_Shortener.Controllers } [HttpDelete("{id:int}")] + [Authorize] public async Task DeleteShortUrlByIdAsync(int id) { var shortUrl = await _db.ShortUrls.FindAsync(id); if (shortUrl == null) return NotFound(); + // TODO: определение пользователя по jwt + _db.ShortUrls.Remove(shortUrl); var clickInfos = _db.ClickInfos.Where(c => c.ShortUrlId == id); diff --git a/URL-Shortener/Program.cs b/URL-Shortener/Program.cs index 1caf70d..80652e9 100644 --- a/URL-Shortener/Program.cs +++ b/URL-Shortener/Program.cs @@ -1,7 +1,10 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Authentication.JwtBearer; using URL_Shortener; using URL_Shortener.Services; using URL_Shortener.Settings; +using Microsoft.IdentityModel.Tokens; +using System.Text; var builder = WebApplication.CreateBuilder(args); @@ -18,7 +21,20 @@ builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.Configure(builder.Configuration.GetSection("AuthSettings")); + +var authSettings = builder.Configuration.GetSection("AuthSettings"); +builder.Services.Configure(authSettings); + +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(o => o.TokenValidationParameters = new() + { + ValidateIssuerSigningKey = true, + ValidateIssuer = true, + ValidateAudience = false, + ValidateLifetime = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes( + authSettings.Get().SecretKey)) + }); builder.Services.AddDbContext(options => { @@ -47,6 +63,7 @@ using (var scope = app.Services.CreateScope()) //app.UseHttpsRedirection(); +app.UseAuthentication(); app.UseAuthorization(); app.MapControllers();