feat: 普通更新

main
bicijinlian 1 year ago
parent b10f913e3b
commit 28ad4d2fa8

@ -10,7 +10,7 @@ namespace AuthStudy.Authentication.Browser
{
public const string SchemeName = "BrowserScheme";
public const string DispayName = "浏览器方案";
public const string DispayName = "浏览器认证方案(基类实现方式)";
public static List<string> AllowBrowsers { get; set; } = new() { "Chrome", "Edge", "Firefox" };

@ -54,8 +54,5 @@ namespace AuthStudy.Authentication.Browser
}
#endregion
#region 基于默认基数的扩展注册
#endregion
}
}

@ -20,14 +20,10 @@ using UAParser;
namespace AuthStudy.Authentication.Browser
{
/// <summary>
/// 浏览器认证处理器:基于默认实现
/// 浏览器认证处理器:基于默认类实现
/// </summary>
public class BrowserAuthenticationHandler : AuthenticationHandler<BrowserAuthenticationOptions>
{
public string DefaultSchemeName = BrowserAuthenticationDefault.SchemeName;
public HttpContext? CurrentHttpContext;
public BrowserAuthenticationHandler
(
IOptionsMonitor<BrowserAuthenticationOptions> 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;

@ -0,0 +1,20 @@
namespace AuthStudy.WebApp
{
/// <summary>
/// 认证方案名称
/// </summary>
public class AuthenticationSchemeList
{
/// <summary>
/// 浏览器认证方案
/// 接口实现方式
/// </summary>
public const string BaseBrowserScheme = "BaseBrowserScheme";
/// <summary>
/// 浏览器认证方案
/// 基类实现方式
/// </summary>
public const string BrowserScheme = "BrowserScheme";
}
}

@ -11,10 +11,6 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Auth\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AuthStudy.Authentication.Basic\AuthStudy.Authentication.Basic.csproj" />
<ProjectReference Include="..\AuthStudy.Authentication.Browser\AuthStudy.Authentication.Browser.csproj" />

@ -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<BrowserAuthenticationOptions, BrowserAuthenticationHandler>("BrowserAuthenticationHandlerByBase", t =>
.AddAuthentication(AuthenticationSchemeList.BaseBrowserScheme)
.AddScheme<BrowserAuthenticationOptions, BrowserAuthenticationHandler>(AuthenticationSchemeList.BaseBrowserScheme, option =>
{
t.AllowBrowsers = new List<string>() { "Edge", "Chrome", "Firefox" };
option.AllowBrowsers = new List<string>() { "Edge", "Chrome", "Firefox" };
});
//默认基类实现注册
#endregion
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
// 配置 Http 管道.
app.UseSwagger();
app.UseSwaggerUI();

Loading…
Cancel
Save