Most működik alapból
This commit is contained in:
@@ -26,6 +26,9 @@ builder.Host.UseSerilog();
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddSingleton<JwtService>();
|
||||
|
||||
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
||||
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
@@ -37,9 +40,9 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = "https://auth.example.com",
|
||||
ValidAudience = "https://mywebapp.com",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("super_secret_key"))
|
||||
ValidIssuer = jwtSettings["Issuer"],
|
||||
ValidAudience = jwtSettings["Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["SecretKey"]))
|
||||
};
|
||||
});
|
||||
|
||||
@@ -62,21 +65,6 @@ app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapPost("/login", async ([FromBody] UserDTO userDTO, JwtService jwtService, HttpContext httpContext) =>
|
||||
{
|
||||
var token = await jwtService.AuthenticateUserAsync(userDTO);
|
||||
if (token is null) return Results.Unauthorized();
|
||||
|
||||
httpContext.Response.Cookies.Append("AuthToken", token, new CookieOptions
|
||||
{
|
||||
HttpOnly = true,
|
||||
Secure = true,
|
||||
SameSite = SameSiteMode.Strict
|
||||
});
|
||||
return Results.Ok(new { Token = token });
|
||||
});
|
||||
|
||||
app.MapGet("/secure-data", [Authorize] () => "This is protected data");
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(context.User?.Identity?.Name) &&
|
||||
|
||||
Reference in New Issue
Block a user