mirror of
https://github.com/ryzij/URL-Shortener.git
synced 2026-07-14 03:56:58 +00:00
Добавил авторизацию
This commit is contained in:
@@ -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<IActionResult> 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);
|
||||
|
||||
@@ -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<HashidsNet.Hashids>();
|
||||
|
||||
builder.Services.AddScoped<UserService>();
|
||||
builder.Services.AddScoped<JwtService>();
|
||||
builder.Services.Configure<AuthSettings>(builder.Configuration.GetSection("AuthSettings"));
|
||||
|
||||
var authSettings = builder.Configuration.GetSection("AuthSettings");
|
||||
builder.Services.Configure<AuthSettings>(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<AuthSettings>().SecretKey))
|
||||
});
|
||||
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
{
|
||||
@@ -47,6 +63,7 @@ using (var scope = app.Services.CreateScope())
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user