fix: 修复错误

main
bicijinlian 2 years ago
parent 0c1ebef1b3
commit b10f913e3b

@ -23,7 +23,6 @@ namespace AuthStudy.Authentication.Browser
/// 浏览器认证处理器:基于默认类型实现 /// 浏览器认证处理器:基于默认类型实现
/// </summary> /// </summary>
public class BrowserAuthenticationHandler : AuthenticationHandler<BrowserAuthenticationOptions> public class BrowserAuthenticationHandler : AuthenticationHandler<BrowserAuthenticationOptions>
{ {
public string DefaultSchemeName = BrowserAuthenticationDefault.SchemeName; public string DefaultSchemeName = BrowserAuthenticationDefault.SchemeName;
public HttpContext? CurrentHttpContext; public HttpContext? CurrentHttpContext;
@ -126,16 +125,26 @@ namespace AuthStudy.Authentication.Browser
{ {
properties?.Parameters.Add("x-itme", "无效的认证"); properties?.Parameters.Add("x-itme", "无效的认证");
CurrentHttpContext!.Response.StatusCode = 401; if (CurrentHttpContext != null)
{
CurrentHttpContext.Response.StatusCode = 401;
if (CurrentHttpContext?.Response.Body.CanWrite ?? false) if (CurrentHttpContext?.Response.Body.CanWrite ?? false)
{ {
var msg = UTF8Encoding.UTF8.GetBytes("认证无效"); var msg = UTF8Encoding.UTF8.GetBytes("认证无效");
var t = CurrentHttpContext!.Response.Body.WriteAsync(msg); 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; return Task.CompletedTask;
} }
protected override Task InitializeHandlerAsync()
{
CurrentHttpContext = base.Context;
return base.InitializeHandlerAsync();
}
private static bool IsMobile(string deviceInfo) private static bool IsMobile(string deviceInfo)
{ {
bool isMobile = false; bool isMobile = false;

@ -16,7 +16,7 @@ namespace AuthStudy.WebApp.Controllers
} }
[Authorize(AuthenticationSchemes = BrowserAuthenticationDefault.SchemeName)] //[Authorize(AuthenticationSchemes = BrowserAuthenticationDefault.SchemeName)]
[Authorize(AuthenticationSchemes = "BrowserAuthenticationHandlerByBase")] [Authorize(AuthenticationSchemes = "BrowserAuthenticationHandlerByBase")]
[HttpGet] [HttpGet]
public IActionResult GetAll() public IActionResult GetAll()

Loading…
Cancel
Save