mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-10 17:59:38 +08:00
48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
using BuildingBlocks.Web;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace BuildingBlocks.Jwt;
|
|
|
|
using Duende.IdentityServer.EntityFramework.Entities;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
public static class JwtExtensions
|
|
{
|
|
public static IServiceCollection AddJwt(this IServiceCollection services)
|
|
{
|
|
var jwtOptions = services.GetOptions<JwtBearerOptions>("Jwt");
|
|
|
|
services.AddAuthentication(o =>
|
|
{
|
|
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddCookie(cfg => cfg.SlidingExpiration = true)
|
|
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
|
|
{
|
|
options.Authority = jwtOptions.Authority;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateAudience = false,
|
|
ClockSkew = TimeSpan.FromSeconds(2) // For prevent add default value (5min) to life time token!
|
|
};
|
|
options.RequireHttpsMetadata = jwtOptions.RequireHttpsMetadata;
|
|
options.MetadataAddress = jwtOptions.MetadataAddress;
|
|
});
|
|
|
|
if (!string.IsNullOrEmpty(jwtOptions.Audience))
|
|
{
|
|
services.AddAuthorization(options =>
|
|
options.AddPolicy(nameof(ApiScope), policy =>
|
|
{
|
|
policy.RequireAuthenticatedUser();
|
|
policy.RequireClaim("scope", jwtOptions.Audience);
|
|
})
|
|
);
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|