c# - .net core 3.0 使用 Policy、AuthenticationSchemes 授权属性
问题描述
我正在尝试在我的应用程序中实现两个身份验证方案。在具有授权属性的控制器中,我设置了控制器必须用来进行身份验证的方案。注册授权:
启动:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureAuthentication();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthentication();
app.UseAuthorization();
}
}
AuthenticationExtensions.cs:
public static class AuthenticationExtensions
{
public static IServiceCollection ConfigureAuthentication(this IServiceCollection services)
{
services.AddAuthentication(AuthConstants.DefaultScheme)
.AddScheme<AuthenticationSchemeOptions, DefaultSchemeHandler>(AuthConstants.DefaultScheme, AuthConstants.DefaultScheme, null)
.AddScheme<AuthenticationSchemeOptions, IdentityAuthenticationHandler>(AuthConstants.IdentityScheme, AuthConstants.IdentityScheme, null);
services.AddAuthorization(options =>
{
options.AddPolicy("IdentityAuthPolicy", policy =>
{
policy.AuthenticationSchemes.Add(AuthConstants.IdentityScheme);
policy.RequireAuthenticatedUser();
});
});
return services;
}
}
在控制器中,我定义了要使用的身份验证方案:
[Authorize(AuthenticationSchemes = AuthConstants.IdentityScheme)]
[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{}
问题:应用程序在失败后尝试使用默认方案进行身份验证,尝试在授权属性中指定的方案。我希望应用程序使用我在授权属性中定义的唯一身份验证方案。另外,我尝试使用策略,但结果是一样的。
解决方案
你应该:
添加控制器服务
通过添加路由相关的中间件来设置路由
先调用
app.UseAuthorization()
before注册授权中间件app.UseAuthentication()
这是您的Startup
课程代码的样子:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AuthConstants.DefaultScheme)
.AddScheme<AuthenticationSchemeOptions, DefaultSchemeHandler>(AuthConstants.DefaultScheme, AuthConstants.DefaultScheme, null)
.AddScheme<AuthenticationSchemeOptions, IdentityAuthenticationHandler>(AuthConstants.IdentityScheme, AuthConstants.IdentityScheme, null);
services.AddAuthorization(options =>
{
options.AddPolicy("IdentityAuthPolicy", policy =>
{
policy.AuthenticationSchemes.Add(AuthConstants.IdentityScheme);
policy.RequireAuthenticatedUser();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
之后,使用AuthenticationSchemes
属性的[Authorize]
属性或其Policy
属性:
[Authorize(AuthenticationSchemes = AuthConstants.IdentityScheme)]
//[Authorize(Policy = "IdentityAuthPolicy")]
[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{
}
推荐阅读
- java - 我将如何在垂直堆栈中来回绘制 4 个正方形?
- swift - 如何对元组数组进行字符串化?
- reactjs - 如何在 React 中传递(和使用)多维数组作为道具?
- architecture - 如何在 Apache Flink 中将作业分配给 TaskManager
- python-2.7 - 响应'对象在运行查找插件'hashi_vault'时没有属性'__getitem__
- amazon-s3 - 使用 pyspark 将 s3 文件从一个存储桶复制到另一个存储桶
- c - 为什么位运算的返回值每次都变化?
- json - 将 Squish 工具报告格式化为机器人框架报告
- javascript - ElectronJS 和 React DevTools:“扩展服务器错误:操作失败:http://localhost:3000/ 没有执行上下文”,来源:devtools://
- docker - 为了提供静态文件,将此功能放入 Docker 映像并成为 Kubernetes 的一部分是一种好习惯吗?