diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs index 8edcf44..0f51835 100644 --- a/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs +++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs @@ -10,7 +10,7 @@ namespace AuthStudy.Authentication.Browser { public const string SchemeName = "BrowserScheme"; - public const string DispayName = "浏览器方案"; + public const string DispayName = "浏览器认证方案(基类实现方式)"; public static List AllowBrowsers { get; set; } = new() { "Chrome", "Edge", "Firefox" }; diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationExtensions.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationExtensions.cs index 7de538c..f1fc63a 100644 --- a/AuthStudy.Authentication.Browser/BrowserAuthenticationExtensions.cs +++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationExtensions.cs @@ -54,8 +54,5 @@ namespace AuthStudy.Authentication.Browser } #endregion - - #region 基于默认基数的扩展注册 - #endregion } } diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs index 5b9b0eb..157df00 100644 --- a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs +++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs @@ -20,14 +20,10 @@ using UAParser; namespace AuthStudy.Authentication.Browser { /// - /// 浏览器认证处理器:基于默认类型实现 + /// 浏览器认证处理器:基于默认基类实现 /// public class BrowserAuthenticationHandler : AuthenticationHandler { - public string DefaultSchemeName = BrowserAuthenticationDefault.SchemeName; - public HttpContext? CurrentHttpContext; - - public BrowserAuthenticationHandler ( IOptionsMonitor options, @@ -52,7 +48,7 @@ namespace AuthStudy.Authentication.Browser properties.Items.Add("AuthenticationBrowser", "浏览器认证属性"); //获取请求浏览器信息,如果请头重复则以后面的为准 - var userAgent = CurrentHttpContext?.Request.Headers["User-Agent"].LastOrDefault(); + var userAgent = Context.Request.Headers["User-Agent"].LastOrDefault(); if (userAgent == null) { properties.UpdateTokenValue("AuthenticationBrowser", "失败:获取不到浏览器信息"); @@ -101,13 +97,13 @@ namespace AuthStudy.Authentication.Browser }; //身份:包含声明集合,是声明集合的包装类,一个身份对应多个声明 - var claimsIdentity = new ClaimsIdentity(Claims, DefaultSchemeName); + var claimsIdentity = new ClaimsIdentity(Claims, Scheme.Name); //当事人/主角:是身份Identity的包装,对应多个身份 var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); //票据:对Principal的包装,一对一 - var ticket = new AuthenticationTicket(claimsPrincipal, DefaultSchemeName); + var ticket = new AuthenticationTicket(claimsPrincipal, Scheme.Name); //认证结果:认证信息会写入 当前请求的 User属性中,供下一个授权中间件使用 result = AuthenticateResult.Success(ticket); @@ -125,26 +121,17 @@ namespace AuthStudy.Authentication.Browser { properties?.Parameters.Add("x-itme", "无效的认证"); - if (CurrentHttpContext != null) + Context.Response.StatusCode = 401; + if (Context?.Response.Body.CanWrite ?? false) { - 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")); + var msg = UTF8Encoding.UTF8.GetBytes("认证无效"); + var t = Context!.Response.Body.WriteAsync(msg); } + Context!.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/Auth/AuthenticationSchemeList.cs b/AuthStudy.WebApp/Auth/AuthenticationSchemeList.cs new file mode 100644 index 0000000..6e78b32 --- /dev/null +++ b/AuthStudy.WebApp/Auth/AuthenticationSchemeList.cs @@ -0,0 +1,20 @@ +namespace AuthStudy.WebApp +{ + /// + /// 认证方案名称 + /// + public class AuthenticationSchemeList + { + /// + /// 浏览器认证方案 + /// 接口实现方式 + /// + public const string BaseBrowserScheme = "BaseBrowserScheme"; + + /// + /// 浏览器认证方案 + /// 基类实现方式 + /// + public const string BrowserScheme = "BrowserScheme"; + } +} diff --git a/AuthStudy.WebApp/AuthStudy.WebApp.csproj b/AuthStudy.WebApp/AuthStudy.WebApp.csproj index bd4a9f5..d369b50 100644 --- a/AuthStudy.WebApp/AuthStudy.WebApp.csproj +++ b/AuthStudy.WebApp/AuthStudy.WebApp.csproj @@ -11,10 +11,6 @@ - - - - diff --git a/AuthStudy.WebApp/Program.cs b/AuthStudy.WebApp/Program.cs index 4d6b01d..0ee7853 100644 --- a/AuthStudy.WebApp/Program.cs +++ b/AuthStudy.WebApp/Program.cs @@ -1,8 +1,6 @@ using AuthStudy.Authentication.Browser; -using Microsoft.AspNetCore.Components.Forms; - namespace AuthStudy.WebApp { public class Program @@ -11,11 +9,9 @@ namespace AuthStudy.WebApp { WebApplicationBuilder builder = WebApplication.CreateBuilder(args); - // Add services to the container. - + // 添加服务到IoC容器 builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); + // Swagger 注册 builder.Services.AddSwaggerGen(); #region 认证注册 @@ -30,18 +26,19 @@ namespace AuthStudy.WebApp } ); builder.Services - .AddAuthentication("BrowserAuthenticationHandlerByBase") - .AddScheme("BrowserAuthenticationHandlerByBase", t => + .AddAuthentication(AuthenticationSchemeList.BaseBrowserScheme) + .AddScheme(AuthenticationSchemeList.BaseBrowserScheme, option => { - t.AllowBrowsers = new List() { "Edge", "Chrome", "Firefox" }; + option.AllowBrowsers = new List() { "Edge", "Chrome", "Firefox" }; }); + //默认基类实现注册 #endregion WebApplication app = builder.Build(); - // Configure the HTTP request pipeline. + // 配置 Http 管道. app.UseSwagger(); app.UseSwaggerUI();