diff --git a/Infrastructure/WebExtensions/JwtExtension.cs b/Infrastructure/WebExtensions/JwtExtension.cs new file mode 100644 index 0000000..bd30ca1 --- /dev/null +++ b/Infrastructure/WebExtensions/JwtExtension.cs @@ -0,0 +1,39 @@ +using Infrastructure; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; +using System; +using System.Threading.Tasks; + +namespace ZR.Infrastructure.WebExtensions +{ + public static class JwtExtension + { + public static void AddJwt(this IServiceCollection services) + { + services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }).AddCookie() + .AddJwtBearer(o => + { + o.TokenValidationParameters = JwtUtil.ValidParameters(); + o.Events = new JwtBearerEvents + { + OnAuthenticationFailed = context => + { + // 如果过期,把过期信息添加到头部 + if (context.Exception.GetType() == typeof(SecurityTokenExpiredException)) + { + Console.WriteLine("jwt过期了"); + context.Response.Headers.Add("Token-Expired", "true"); + } + + return Task.CompletedTask; + }, + }; + }); + } + } +} diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 7c3e820..617280b 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -1,8 +1,6 @@ using AspNetCoreRateLimit; using Infrastructure.Converter; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.DataProtection; -using Microsoft.IdentityModel.Tokens; using System.Text.Json.Serialization; using ZR.Admin.WebApi.Extensions; using ZR.Common.Cache; @@ -38,31 +36,10 @@ builder.Services.AddHttpContextAccessor(); builder.Services.Configure(builder.Configuration); //jwt 认证 -builder.Services.AddAuthentication(options => -{ - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; -}).AddCookie() -.AddJwtBearer(o => -{ - o.TokenValidationParameters = JwtUtil.ValidParameters(); - o.Events = new JwtBearerEvents - { - OnAuthenticationFailed = context => - { - // 如果过期,把过期信息添加到头部 - if (context.Exception.GetType() == typeof(SecurityTokenExpiredException)) - { - Console.WriteLine("jwt过期了"); - context.Response.Headers.Add("Token-Expired", "true"); - } - - return Task.CompletedTask; - }, - }; -}); - +builder.Services.AddJwt(); +//配置文件 builder.Services.AddSingleton(new AppSettings(builder.Configuration)); +//app服务注册 builder.Services.AddAppService(); //开启计划任务 builder.Services.AddTaskSchedulers();