From 5d7c56c1a8186c1cd9826fc26a8ee789a1c6891e Mon Sep 17 00:00:00 2001
From: bicijinlian <bicijinlian@163.com>
Date: Sun, 11 Jun 2023 20:02:45 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                                       |  3 +++
 .../BrowserAuthenticationHandler.cs              | 16 ++++++++--------
 AuthStudy.WebApp/Program.cs                      |  8 ++------
 3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore
index eda3f21..ebfd29e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -438,3 +438,6 @@ FodyWeavers.xsd
 # Built Visual Studio Code Extensions
 *.vsix
 
+#Rider
+.idea
+/.idea
diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs
index 5186b6a..d2be67a 100644
--- a/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs
+++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationHandler.cs
@@ -92,7 +92,7 @@ namespace AuthStudy.Authentication.Browser
             var device = new Claim("Device", clientInfo.Device.ToString());     //设备                                                                                           //设备
 
             //声明集合
-            var Claims = new List<Claim>
+            var claims = new List<Claim>
             {
                 browser,
                 os,
@@ -100,7 +100,7 @@ namespace AuthStudy.Authentication.Browser
             };
 
             //身份:包含声明集合,是声明集合的包装类,一个身份对应多个声明
-            var claimsIdentity = new ClaimsIdentity(Claims, DefaultSchemeName);
+            var claimsIdentity = new ClaimsIdentity(claims, DefaultSchemeName);
 
             //当事人/主角:是身份Identity的包装,对应多个身份
             var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
@@ -118,16 +118,16 @@ namespace AuthStudy.Authentication.Browser
         }
 
         /// <summary>
-        /// 无认证:服务端向客户端(浏览器)发质询(要求提供一个新票据),质询体现为 htpp请求的响应。
+        /// 无认证:服务端向客户端(浏览器)发质询(要求提供一个新票据),质询体现为 http请求的响应。
         /// </summary>
         public async Task ChallengeAsync(AuthenticationProperties? properties)
         {
-            properties?.Parameters.Add("x-itme", "无效的认证");
+            properties?.Parameters.Add("x-item", "无效的认证");
 
             CurrentHttpContext!.Response.StatusCode = 401;
             if (CurrentHttpContext?.Response.Body.CanWrite ?? false)
             {
-                var msg = UTF8Encoding.UTF8.GetBytes("认证无效");
+                var msg = Encoding.UTF8.GetBytes("认证无效");
                 await CurrentHttpContext!.Response.Body.WriteAsync(msg);
             }
             CurrentHttpContext?.Items.Add("认证结束时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
@@ -135,14 +135,14 @@ namespace AuthStudy.Authentication.Browser
         }
 
         /// <summary>
-        /// 无权限:服务端向客户端(浏览器)发质询(要求提供一个新票据),质询体现为 htpp请求的响应。
+        /// 无权限:服务端向客户端(浏览器)发质询(要求提供一个新票据),质询体现为 http请求的响应。
         /// </summary>
         public async Task ForbidAsync(AuthenticationProperties? properties)
         {
             CurrentHttpContext!.Response.StatusCode = 403;
             if (CurrentHttpContext?.Response.Body.CanWrite ?? false)
             {
-                var msg = UTF8Encoding.UTF8.GetBytes("无权访问");
+                var msg = Encoding.UTF8.GetBytes("无权访问");
                 await CurrentHttpContext!.Response.Body.WriteAsync(msg);
             }
             //return Task.CompletedTask;
@@ -160,7 +160,7 @@ namespace AuthStudy.Authentication.Browser
         /// <summary>
         /// 初始化
         /// </summary>
-        public async Task InitializeAsync(AuthenticationScheme scheme, Microsoft.AspNetCore.Http.HttpContext context)
+        public async Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
         {
             //初始化工作,传递给认证方法和授权中间件
             CurrentHttpContext = context;
diff --git a/AuthStudy.WebApp/Program.cs b/AuthStudy.WebApp/Program.cs
index 5d4e8f8..abcfb7f 100644
--- a/AuthStudy.WebApp/Program.cs
+++ b/AuthStudy.WebApp/Program.cs
@@ -7,7 +7,7 @@ namespace AuthStudy.WebApp
     {
         public static void Main(string[] args)
         {
-            WebApplicationBuilder? builder = WebApplication.CreateBuilder(args);
+            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
             // Add services to the container.
 
@@ -17,10 +17,6 @@ namespace AuthStudy.WebApp
             builder.Services.AddSwaggerGen();
 
             #region 认证注册
-            //builder.Services.AddAuthentication(configOption =>
-            //{
-            //    configOption.AddScheme<BrowserAuthenticationHandler2<BrowserAuthenticationOptions>>(BrowserAuthenticationDefault.SchemeName, BrowserAuthenticationDefault.DispayName);
-            //});
             builder.Services.AddBrowserAuthentication
             (
                 BrowserAuthenticationDefault.SchemeName,
@@ -32,7 +28,7 @@ namespace AuthStudy.WebApp
             );
             #endregion
 
-            WebApplication? app = builder.Build();
+            WebApplication app = builder.Build();
 
             // Configure the HTTP request pipeline.
             app.UseSwagger();