c# - How to use multiple authentication schemes with an AuthorizationFilter Attribute
问题描述
I have a multiple authentication schemes to avoid the token provided by my Identity Server and Azure Active Directory
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://localhost:5000/";
options.Audience = "api1";
options.Events = new JwtBearerEvents
{
OnTokenValidated = async context =>
{
TokenValidated(context);
},
OnAuthenticationFailed = context =>
{
context.Fail("error");
return Task.CompletedTask;
}
};
})
.AddJwtBearer(AzureADDefaults.BearerAuthenticationScheme, options =>
{
options.Audience = myAudience;
options.Authority = "https://login.microsoftonline.com/" + TenantId;
options.Events = new JwtBearerEvents
{
OnTokenValidated = async context =>
{
TokenValidated(context);
},
OnAuthenticationFailed = context =>
{
context.Fail("error");
return Task.CompletedTask;
}
};
});
}
I have an attribute for authorize the controllers of the API:
public void OnAuthorization(AuthorizationFilterContext context)
{
if (context.Filters.Any(item => item is IAllowAnonymousFilter))
{
return;
}
if (!isAuthorized(context))
{
//...
}
}
My problems is the sort of the execution. If I send a request with a token provided by ADD, at first, the request arrive at the first Bearer and go to OnAuthenticationFailed because it is not provided by IS. Then, the execution go to OnAuthorization() and then return to the second AddJwtBearer and enter on OnTokenValidated().
I need resolve the second authentication squema before the OnAuthorize of the attribute is executes.
How can I do that?
解决方案
推荐阅读
- c# - Spotify api 检索到的访问令牌不起作用
- laravel-5 - 如何在laravel中修复“试图获取非对象的属性'id'”?
- marklogic - 如何为 webdav 连接到 marklogic 创建只读用户
- css - 无法使用 Less mixin 创建动态类
- javascript - 我可以让 src 属性从服务器或调整服务器获取 base64 数据吗?
- excel - 是否有关于如何通过 Excel 中的 VBA 驱动 Office“公式编辑器”的文档?
- android - Scarlet Web 套接字库连接失败
- rust - rust statrs 样本不在范围内?
- python - 如何在 Python 中将浮点列表转换为 int 对列表?
- reactjs - 反应原生
里面 不包装