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()