Compare commits

..

2 Commits

Author SHA1 Message Date
ryzij da47d7fdf3 Добавил определение пользователя 2026-06-17 02:59:06 +03:00
ryzij d659a0289f Небольшое изменение 2026-06-17 02:58:52 +03:00
2 changed files with 7 additions and 4 deletions
@@ -4,6 +4,7 @@ using URL_Shortener.DTO;
using URL_Shortener.Models; using URL_Shortener.Models;
using HashidsNet; using HashidsNet;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
namespace URL_Shortener.Controllers namespace URL_Shortener.Controllers
{ {
@@ -86,7 +87,9 @@ namespace URL_Shortener.Controllers
if (shortUrl == null) if (shortUrl == null)
return NotFound(); return NotFound();
// TODO: определение пользователя по jwt if (!int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier), out var userId) ||
shortUrl.UserId != userId)
throw new Exception("Not authorized");
_db.ShortUrls.Remove(shortUrl); _db.ShortUrls.Remove(shortUrl);
+3 -3
View File
@@ -14,9 +14,9 @@ namespace URL_Shortener.Services
public string GenerateToken(User user) public string GenerateToken(User user)
{ {
var claims = new List<Claim>() { var claims = new List<Claim>() {
new("userName", user.Name), new(ClaimTypes.Name, user.Name),
new("userEmail", user.Email), new(ClaimTypes.Email, user.Email),
new("id", user.Id.ToString()) new(ClaimTypes.NameIdentifier, user.Id.ToString())
}; };
var jwt = new JwtSecurityToken( var jwt = new JwtSecurityToken(
expires: DateTime.UtcNow.Add(options.Value.Expires), expires: DateTime.UtcNow.Add(options.Value.Expires),