From b10f913e3b6fb857fafab172e380c1d5de9157c1 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Mon, 12 Jun 2023 12:25:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BrowserAuthenticationHandler.cs | 21 +++++++++++++------ .../Controllers/AccountsController.cs | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs index bcdeeed..5b9b0eb 100644 --- a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs +++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs @@ -23,7 +23,6 @@ namespace AuthStudy.Authentication.Browser /// 浏览器认证处理器:基于默认类型实现 /// public class BrowserAuthenticationHandler : AuthenticationHandler - { public string DefaultSchemeName = BrowserAuthenticationDefault.SchemeName; public HttpContext? CurrentHttpContext; @@ -126,16 +125,26 @@ namespace AuthStudy.Authentication.Browser { properties?.Parameters.Add("x-itme", "无效的认证"); - CurrentHttpContext!.Response.StatusCode = 401; - if (CurrentHttpContext?.Response.Body.CanWrite ?? false) + if (CurrentHttpContext != null) { - var msg = UTF8Encoding.UTF8.GetBytes("认证无效"); - var t = CurrentHttpContext!.Response.Body.WriteAsync(msg); + CurrentHttpContext.Response.StatusCode = 401; + if (CurrentHttpContext?.Response.Body.CanWrite ?? false) + { + var msg = UTF8Encoding.UTF8.GetBytes("认证无效"); + var t = CurrentHttpContext!.Response.Body.WriteAsync(msg); + } + CurrentHttpContext!.Items.Add("认证结束时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } - CurrentHttpContext?.Items.Add("认证结束时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + return Task.CompletedTask; } + protected override Task InitializeHandlerAsync() + { + CurrentHttpContext = base.Context; + return base.InitializeHandlerAsync(); + } + private static bool IsMobile(string deviceInfo) { bool isMobile = false; diff --git a/AuthStudy.WebApp/Controllers/AccountsController.cs b/AuthStudy.WebApp/Controllers/AccountsController.cs index a181931..99eb7d6 100644 --- a/AuthStudy.WebApp/Controllers/AccountsController.cs +++ b/AuthStudy.WebApp/Controllers/AccountsController.cs @@ -16,7 +16,7 @@ namespace AuthStudy.WebApp.Controllers } - [Authorize(AuthenticationSchemes = BrowserAuthenticationDefault.SchemeName)] + //[Authorize(AuthenticationSchemes = BrowserAuthenticationDefault.SchemeName)] [Authorize(AuthenticationSchemes = "BrowserAuthenticationHandlerByBase")] [HttpGet] public IActionResult GetAll()