|
|
@ -3,8 +3,11 @@ using AspNetCoreApi.DbContexts; |
|
|
|
using AspNetCoreApi.Middlewares; |
|
|
|
using AspNetCoreApi.Middlewares; |
|
|
|
using AspNetCoreApi.Models; |
|
|
|
using AspNetCoreApi.Models; |
|
|
|
using AspNetCoreApi.Services; |
|
|
|
using AspNetCoreApi.Services; |
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens; |
|
|
|
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
|
|
namespace AspNetCoreApi |
|
|
|
namespace AspNetCoreApi |
|
|
|
{ |
|
|
|
{ |
|
|
@ -32,6 +35,29 @@ namespace AspNetCoreApi |
|
|
|
.AddEntityFrameworkStores<AppDbContext>() |
|
|
|
.AddEntityFrameworkStores<AppDbContext>() |
|
|
|
.AddDefaultTokenProviders(); |
|
|
|
.AddDefaultTokenProviders(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// JWT Authorization |
|
|
|
|
|
|
|
string jwtKey = "ThisIsMyMyJWTKey1234!ThisIsMyMyJWTKey1234!"; |
|
|
|
|
|
|
|
builder.Services.AddAuthentication(options => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
|
|
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.AddJwtBearer(options => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ValidateIssuer = true, |
|
|
|
|
|
|
|
ValidateAudience = true, |
|
|
|
|
|
|
|
ValidateLifetime = true, |
|
|
|
|
|
|
|
ValidateIssuerSigningKey = true, |
|
|
|
|
|
|
|
ValidIssuer = "MyIssuer", |
|
|
|
|
|
|
|
ValidAudience = "MyAudience", |
|
|
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddScoped<JWTAuthenticationService>(); |
|
|
|
|
|
|
|
|
|
|
|
// Add services to the container. |
|
|
|
// Add services to the container. |
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers(); |
|
|
|
builder.Services.AddControllers(); |
|
|
@ -55,6 +81,7 @@ namespace AspNetCoreApi |
|
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection(); |
|
|
|
app.UseHttpsRedirection(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication(); |
|
|
|
app.UseAuthorization(); |
|
|
|
app.UseAuthorization(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|