From 844a7b455ccc0576d4925ee50a1721312534cd55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Thu, 15 Sep 2022 18:40:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=89=B4=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TestController.cs | 7 ++++ .../Yi.Framework.ApiMicroservice/Program.cs | 2 +- .../Yi.Framework.WebCore/CommonExtend.cs | 2 +- .../MiddlewareExtend/JwtExtension.cs | 33 +++++++++++++++---- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs index b52deaf4..6e8cdb28 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs @@ -197,5 +197,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers var treeData = Common.Helper.TreeHelper.SetTree(vueRouterModels); return Result.Success().SetData(treeData); } + + [Authorize] + [HttpGet] + public Result AuthorizeTest() + { + return Result.Success(); + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index 33507d35..78745b74 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -1,3 +1,4 @@ +global using System; using Autofac.Extensions.DependencyInjection; using Yi.Framework.WebCore.BuilderExtend; using Yi.Framework.Core; @@ -11,7 +12,6 @@ using Yi.Framework.WebCore.AttributeExtend; using Yi.Framework.WebCore.SignalRHub; - var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddCommandLine(args); builder.WebHost.UseUrls(builder.Configuration.GetValue("StartUrl")); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs index ebc23248..a7a81169 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs @@ -38,7 +38,7 @@ namespace Yi.Framework.WebCore long resId = 0; try { - claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims; + claimlist = httpContext.User.Claims; resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value); } catch diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/JwtExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/JwtExtension.cs index 451b3496..fc73ef92 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/JwtExtension.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/JwtExtension.cs @@ -5,9 +5,11 @@ using Microsoft.IdentityModel.Tokens; using System; using System.IO; using System.Text; +using System.Threading.Tasks; using Yi.Framework.Common.Const; using Yi.Framework.Common.Helper; using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Common.Models; using Yi.Framework.Core; namespace Yi.Framework.WebCore.MiddlewareExtend @@ -25,15 +27,32 @@ namespace Yi.Framework.WebCore.MiddlewareExtend services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { + options.Events = new JwtBearerEvents + { + OnAuthenticationFailed = (context) => + { + return Task.CompletedTask; + }, + OnMessageReceived = (context) => + { + return Task.CompletedTask; + }, + OnChallenge = (context) => + { + return Task.CompletedTask; + }, + }; + options.TokenValidationParameters = new TokenValidationParameters { - ValidateIssuer = true,//是否验证Issuer - ValidateAudience = true,//是否验证Audience - ValidateLifetime = true,//是否验证失效时间 - ValidateIssuerSigningKey = true,//是否验证SecurityKey - ValidAudience = jwtOptions.Audience,//Audience - ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致 - IssuerSigningKey = new RsaSecurityKey(RSAFileHelper.GetPublicKey()) + ClockSkew = TimeSpan.Zero,//过期缓冲时间 + ValidateIssuer = true,//是否验证Issuer + ValidateAudience = true,//是否验证Audience + ValidateLifetime = true,//是否验证失效时间 + ValidateIssuerSigningKey = true,//是否验证SecurityKey + ValidAudience = jwtOptions.Audience,//Audience + ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致 + IssuerSigningKey = new RsaSecurityKey(RSAFileHelper.GetPublicKey()) }; }); return services;