From 6b8a8fecab6de4b594ccde4f4f47a37d463dd969 Mon Sep 17 00:00:00 2001
From: bicijinlian <bicijinlian@163.com>
Date: Sun, 18 Jun 2023 02:07:35 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../BrowserAuthenticationDefault.cs           | 10 ++-----
 .../Controllers/AccountsController.cs         | 26 +++++++++----------
 AuthStudy.WebApp/Program.cs                   | 12 ++++-----
 Docs/说明.md                                |  5 ++++
 4 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs b/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs
index 0f51835..4a8d294 100644
--- a/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs
+++ b/AuthStudy.Authentication.Browser/BrowserAuthenticationDefault.cs
@@ -1,16 +1,10 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AuthStudy.Authentication.Browser
+namespace AuthStudy.Authentication.Browser
 {
     public static class BrowserAuthenticationDefault
     {
         public const string SchemeName = "BrowserScheme";
 
-        public const string DispayName = "浏览器认证方案(基类实现方式)";
+        public const string DisplayName = "浏览器认证方案(基类实现方式)";
 
         public static List<string> AllowBrowsers { get; set; } = new() { "Chrome", "Edge", "Firefox" };
 
diff --git a/AuthStudy.WebApp/Controllers/AccountsController.cs b/AuthStudy.WebApp/Controllers/AccountsController.cs
index df6a355..9fc80f2 100644
--- a/AuthStudy.WebApp/Controllers/AccountsController.cs
+++ b/AuthStudy.WebApp/Controllers/AccountsController.cs
@@ -26,20 +26,20 @@ namespace AuthStudy.WebApp.Controllers
         //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
         //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BaseBrowserScheme},{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
         [HttpGet]
-        public async Task<IActionResult> GetAll()
+        public IActionResult GetAll()
         {
-            var authenticateResult = await HttpContext.AuthenticateAsync();
-            if (authenticateResult.Succeeded)
-            {
-                _logger.LogInformation("认证成功");
-            }
-            else
-            {
-                Response.StatusCode = 401;
-                _logger.LogInformation("认证失败");
-                return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message};
-            }
-
+            // var authenticateResult = await HttpContext.AuthenticateAsync();
+            // if (authenticateResult.Succeeded)
+            // {
+            //     _logger.LogInformation("认证成功");
+            // }
+            // else
+            // {
+            //     Response.StatusCode = 401;
+            //     _logger.LogInformation("认证失败");
+            //     return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message};
+            // }
+            
             //输出认证信息
             foreach (var claim in User.Claims)
             {
diff --git a/AuthStudy.WebApp/Program.cs b/AuthStudy.WebApp/Program.cs
index 2e50638..be05ad2 100644
--- a/AuthStudy.WebApp/Program.cs
+++ b/AuthStudy.WebApp/Program.cs
@@ -1,6 +1,4 @@
-
 using System.Security.Claims;
-
 using AuthStudy.Authentication.Basic;
 using AuthStudy.Authentication.Basic.Events;
 using AuthStudy.Authentication.Browser;
@@ -14,7 +12,8 @@ namespace AuthStudy.WebApp
             WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
             
             // 添加服务到IoC容器
-            builder.Services.AddControllers();
+            builder.Services.AddControllers(); //这里已经调用过了基础的认证与授权方法
+
             // Swagger 注册
             builder.Services.AddSwaggerGen();
 
@@ -23,7 +22,7 @@ namespace AuthStudy.WebApp
             builder.Services.AddBrowserAuthentication
             (
                 BrowserAuthenticationDefault.SchemeName,
-                BrowserAuthenticationDefault.DispayName,
+                BrowserAuthenticationDefault.DisplayName,
                 new BrowserAuthenticationOptions()
                 {
                     AllowBrowsers = new List<string>() { "Edge" }
@@ -37,7 +36,7 @@ namespace AuthStudy.WebApp
                     option.AllowBrowsers = new List<string>() { "Edge", "Chrome", "Firefox" };
                 })
                 //基本认证
-                .AddBasic(options =>
+                .AddBasic(BasicAuthenticationDefaults.AuthenticationScheme,options =>
                 {
                     options.Realm = "Basic Authentication";
                     options.Events = new BasicAuthenticationEvents
@@ -60,7 +59,8 @@ namespace AuthStudy.WebApp
                         }
                     };
 
-                });
+                })
+                ;
 
             //默认基类实现注册
 
diff --git a/Docs/说明.md b/Docs/说明.md
index 0aac100..170c35e 100644
--- a/Docs/说明.md
+++ b/Docs/说明.md
@@ -86,3 +86,8 @@
 ## 认证使用方式
 + 配合授权一起使用:api控制器或方法上加特性[Authorize],由框架自动调用
 + 在Api方法内部调用 HttpContext 扩展方法: `var result = HttpContext.AuthenticateAsync();` 拿到认证结果,手动执行自己的逻辑。
+
+## 关于IoC手动获取对象
++ GetService<T>()方法:如果对象未注册,则返回 null 对象
++ GetRequiredService<T>()方法:如果对象未注册,则抛出异常
+