gpt4 book ai didi

c# - Asp.Net Core 自定义认证中间件方案

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:42 26 4
gpt4 key购买 nike

我的项目在同一个解决方案中有一个前端和一个 API,因此我正在构建一个自定义身份验证中间件,因为前端 Controller 使用 Identity,它旨在仅授权 API Controller 。

我像这样在 Startup.cs 上为 Identity 的 AutomaticAuthenticate 选项设置 false

services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.User.RequireUniqueEmail = true;
config.Password.RequiredLength = 4;
config.Cookies.ApplicationCookie.AuthenticationScheme = "Cookie";
config.Cookies.ApplicationCookie.AutomaticAuthenticate = false;
}).AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();

对于我的自定义中间件,我实现了自定义选项并且我已经做到了

app.UseApiAuthorizationMiddleware(new ApiAuthenticationOptions
{
AuthenticationScheme = "Api",
AutomaticAuthenticate = false
});

现在当我尝试使用

[Authorize(AuthenticationSchemes = "Api")]

在我的 Controller 中,Visul Studio 告诉我无法找到 AuthenticationSchemes 并且我丢失了并组装或引用。快速操作工具告诉我添加

"System.Net.Primitives": "4.0.11-rc2-24027"

但我最终得到的是 4.0.10 版,但它无法编译...我不确定到目前为止我所做的是否很好,或者这里是否确实存在问题。

我希望这很清楚。

谢谢

最佳答案

为了正确实现自定义身份验证中间件,我不仅要实现从 AuthenticationMiddleware 继承的中间件,还要实现从 AuthenticationHandler 扩展的身份验证处理程序,所有逻辑都在 HandleAuthenticateAsync() 方法上进行

关于c# - Asp.Net Core 自定义认证中间件方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com