main
bicijinlian 2 years ago
parent 981209b4a5
commit 6b8a8fecab

@ -1,16 +1,10 @@
using System; namespace AuthStudy.Authentication.Browser
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AuthStudy.Authentication.Browser
{ {
public static class BrowserAuthenticationDefault public static class BrowserAuthenticationDefault
{ {
public const string SchemeName = "BrowserScheme"; public const string SchemeName = "BrowserScheme";
public const string DispayName = "浏览器认证方案(基类实现方式)"; public const string DisplayName = "浏览器认证方案(基类实现方式)";
public static List<string> AllowBrowsers { get; set; } = new() { "Chrome", "Edge", "Firefox" }; public static List<string> AllowBrowsers { get; set; } = new() { "Chrome", "Edge", "Firefox" };

@ -26,19 +26,19 @@ namespace AuthStudy.WebApp.Controllers
//[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")] //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
//[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BaseBrowserScheme},{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")] //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BaseBrowserScheme},{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAll() public IActionResult GetAll()
{ {
var authenticateResult = await HttpContext.AuthenticateAsync(); // var authenticateResult = await HttpContext.AuthenticateAsync();
if (authenticateResult.Succeeded) // if (authenticateResult.Succeeded)
{ // {
_logger.LogInformation("认证成功"); // _logger.LogInformation("认证成功");
} // }
else // else
{ // {
Response.StatusCode = 401; // Response.StatusCode = 401;
_logger.LogInformation("认证失败"); // _logger.LogInformation("认证失败");
return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message}; // return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message};
} // }
//输出认证信息 //输出认证信息
foreach (var claim in User.Claims) foreach (var claim in User.Claims)

@ -1,6 +1,4 @@
using System.Security.Claims; using System.Security.Claims;
using AuthStudy.Authentication.Basic; using AuthStudy.Authentication.Basic;
using AuthStudy.Authentication.Basic.Events; using AuthStudy.Authentication.Basic.Events;
using AuthStudy.Authentication.Browser; using AuthStudy.Authentication.Browser;
@ -14,7 +12,8 @@ namespace AuthStudy.WebApp
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// 添加服务到IoC容器 // 添加服务到IoC容器
builder.Services.AddControllers(); builder.Services.AddControllers(); //这里已经调用过了基础的认证与授权方法
// Swagger 注册 // Swagger 注册
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
@ -23,7 +22,7 @@ namespace AuthStudy.WebApp
builder.Services.AddBrowserAuthentication builder.Services.AddBrowserAuthentication
( (
BrowserAuthenticationDefault.SchemeName, BrowserAuthenticationDefault.SchemeName,
BrowserAuthenticationDefault.DispayName, BrowserAuthenticationDefault.DisplayName,
new BrowserAuthenticationOptions() new BrowserAuthenticationOptions()
{ {
AllowBrowsers = new List<string>() { "Edge" } AllowBrowsers = new List<string>() { "Edge" }
@ -37,7 +36,7 @@ namespace AuthStudy.WebApp
option.AllowBrowsers = new List<string>() { "Edge", "Chrome", "Firefox" }; option.AllowBrowsers = new List<string>() { "Edge", "Chrome", "Firefox" };
}) })
//基本认证 //基本认证
.AddBasic(options => .AddBasic(BasicAuthenticationDefaults.AuthenticationScheme,options =>
{ {
options.Realm = "Basic Authentication"; options.Realm = "Basic Authentication";
options.Events = new BasicAuthenticationEvents options.Events = new BasicAuthenticationEvents
@ -60,7 +59,8 @@ namespace AuthStudy.WebApp
} }
}; };
}); })
;
//默认基类实现注册 //默认基类实现注册

@ -86,3 +86,8 @@
## 认证使用方式 ## 认证使用方式
+ 配合授权一起使用api控制器或方法上加特性[Authorize],由框架自动调用 + 配合授权一起使用api控制器或方法上加特性[Authorize],由框架自动调用
+ 在Api方法内部调用 HttpContext 扩展方法: `var result = HttpContext.AuthenticateAsync();` 拿到认证结果,手动执行自己的逻辑。 + 在Api方法内部调用 HttpContext 扩展方法: `var result = HttpContext.AuthenticateAsync();` 拿到认证结果,手动执行自己的逻辑。
## 关于IoC手动获取对象
+ GetService<T>()方法:如果对象未注册,则返回 null 对象
+ GetRequiredService<T>()方法:如果对象未注册,则抛出异常

Loading…
Cancel
Save