![](/img/trans.png)
[英]ASP.NET Core 2.0 - AddAuthentication and DefaultScheme
[英]How to use multiple Aws Cognito Userpools in AddAuthentication() in a ASP.NET CORE 2.1 API
我目前遇到的问题是我有一个ASP.NET CORE API,我们在其中使用Aws Cognito用户授权对该API的访问,并且运行良好。 现在,我们需要使用第二个用户池进行授权,以便两个不同的用户池可以访问API。 我在如何在AddAuthentication / AddJwtBearer中添加多个听觉对象/权限时遇到了一些麻烦,似乎这些方法不能采用名称相同的两个模式“ Bearer”,有人知道该怎么做吗?
我已经尝试添加多个AddJwtBearer,这会使API崩溃。 尝试了不同的方法,但是大多数解决方案使用了多个受众,但是从没有同时谈论多个权限(如果有道理)。
目前喜欢这样
services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.Audience = "AUDIENCE";
options.Authority = "AUTHORITY";
});
我想要这样的东西
services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.Audience = "AUDIENCE";
options.Authority = "AUTHORITY";
})
.AddJwtBearer(options =>
{
options.Audience = "AUDIENCE2";
options.Authority = "AUTHORITY2";
});
我的目标是希望两个用户池的用户都可以授权使用该API,在此先感谢您的帮助!
您必须像以前一样添加多个承载,但是要使用不同的名称。
services
.AddAuthentication()
.AddJwtBearer("Bearer1", options =>
{
options.Audience = "AUDIENCE";
options.Authority = "AUTHORITY";
})
.AddJwtBearer("Bearer2", options =>
{
options.Audience = "AUDIENCE2";
options.Authority = "AUTHORITY2";
});
然后,您必须添加自己的自定义中间件,而不是稍后使用UseAuthentication()
。
app.Use(async (context, next) =>
{
var result = await context.AuthenticateAsync("Bearer1");
if (!result.Succeeded)
{
result = await context.AuthenticateAsync("Bearer2");
}
context.User = result.Principal;
await next();
});
我自己还没有尝试过,只是将其与评论结合起来,所以让我知道它是否对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.