From 514f0771aa58aefe80a193a2e45937a3c514d916 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Sun, 17 Jul 2022 14:34:21 +0300 Subject: [PATCH] chore: add dependencies for Authentification & Authorization --- Server/Program.cs | 49 ++++++++++++++++++++++++++--- Server/Server.csproj | 3 ++ Server/appsettings.Development.json | 5 +++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index 53f7cd4..f889335 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,4 +1,8 @@ +using System.Text; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Server.Data; @@ -13,7 +17,43 @@ builder.Services.AddControllers().AddNewtonsoftJson(o => { o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); + +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o => { + o.TokenValidationParameters = new TokenValidationParameters { + ValidateActor = true, + ValidateAudience = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + ValidIssuer = builder.Configuration["Jwt:Issuer"], + ValidAudience = builder.Configuration["Jwt:Audience"], + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:key"])) + }; +}); +builder.Services.AddAuthorization(); + +builder.Services.AddSwaggerGen(o => { + o.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { + Scheme = "Bearer", + BearerFormat = "JWT", + In = ParameterLocation.Header, + Name = "Authorization", + Description = "Bearer Authentication with JWT Token", + Type = SecuritySchemeType.Http + }); + o.AddSecurityRequirement(new OpenApiSecurityRequirement { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Id = "Bearer", + Type = ReferenceType.SecurityScheme + } + }, + new List() + } + }); +}); builder.Services.AddDbContext(o => o.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"))); @@ -28,10 +68,9 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } -app.UseHttpsRedirection(); - -app.UseAuthorization(); - app.MapControllers(); +app.UseAuthentication(); +app.UseAuthorization(); + app.Run(); \ No newline at end of file diff --git a/Server/Server.csproj b/Server/Server.csproj index b65b032..59ef699 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -7,14 +7,17 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Server/appsettings.Development.json b/Server/appsettings.Development.json index 79f92cd..c55ea13 100644 --- a/Server/appsettings.Development.json +++ b/Server/appsettings.Development.json @@ -7,5 +7,10 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "Jwt": { + "Key": "W{H)QNdxz2.pES&8VpCT_iGchNCpJqV?iDc=Sx(dAM@{nh+%*ge-]%V[BRq$]rDd", + "Issuer": "https://localhost:7248", + "Audience": "https://localhost:7248" } }