C# 如何在创建访问群体时将访问群体添加到JWTBeareAuthenticationOptions?

C# 如何在创建访问群体时将访问群体添加到JWTBeareAuthenticationOptions?,c#,asp.net-web-api,oauth-2.0,jwt,C#,Asp.net Web Api,Oauth 2.0,Jwt,我正在使用oauth2和jwt令牌保护我的公共api。我将auth服务器配置为发出jwt令牌,但我正在努力实现'ConfigureOAuthTokenConsumption'方法 此代码与我目前使用的代码非常相似,仅供参考: private static void ConfigureOAuthTokenConsumption(IAppBuilder app) { var issuer = _baseUrl; // The URL of the service issuing the JW

我正在使用oauth2和jwt令牌保护我的公共api。我将auth服务器配置为发出jwt令牌,但我正在努力实现'ConfigureOAuthTokenConsumption'方法

此代码与我目前使用的代码非常相似,仅供参考:

private static void ConfigureOAuthTokenConsumption(IAppBuilder app)
{
    var issuer = _baseUrl; // The URL of the service issuing the JWT Token. 
    var audienceRepo = ActiveKernel.Get<IAudienceRepo>();
    var validAudiences = audienceRepo.Get();

    List<string> allowedAudienceIds = new List<string>();
    List<IIssuerSecurityTokenProvider> providers = new List<IIssuerSecurityTokenProvider>();

    // Build the list of valid audiences and keys
    foreach (var validAudience in validAudiences)
    {
        allowedAudienceIds.Add(validAudience.ClientId.ToString().ToUpper());
        providers.Add(new SymmetricKeyIssuerSecurityTokenProvider(issuer, TextEncodings.Base64Url.Decode(validAudience.ClientSecret)));
    }



    // Api controllers with an[Authorize] attribute will be validated with JWT
    app.UseJwtBearerAuthentication(
        new JwtBearerAuthenticationOptions
        {
            AuthenticationMode = AuthenticationMode.Active,
            AllowedAudiences = allowedAudienceIds.ToArray(),
            IssuerSecurityTokenProviders = providers.ToArray()
        });
}
private static void ConfigureOAuthTokenConsumption(IAppBuilder应用程序)
{
var issuer=\u baseUrl;//发出JWT令牌的服务的URL。
var audencerepo=ActiveKernel.Get();
var validudiences=audencerepo.Get();
List allowedAudienceIds=新列表();
列表提供程序=新列表();
//构建有效访问群体和密钥的列表
foreach(有效期中的var有效期)
{
AllowedAudienceId.Add(validudience.ClientId.ToString().ToUpper());
添加(新的SymmetriceIsuerSecurityTokenProvider(颁发者,textcodections.Base64Url.Decode(validudience.ClientSecret));
}
//具有[Authorize]属性的Api控制器将使用JWT进行验证
app.UseJwtBearerAuthentication(
新的JWTBeareAuthenticationOptions
{
AuthenticationMode=AuthenticationMode.Active,
AllowedAudients=allowedAudienceIds.ToArray(),
IssuerSecurityTokenProviders=providers.ToArray()
});
}
在这种方法中,我加载了我所有的受众,这对于已有的受众来说非常有效。如果在我的服务器上注册了新的访问者,则在应用程序重新启动之前,他们将无法访问


如何在创建访问群体时将其添加到JWTBeareAuthenticationOptions中?

您是否找到了解决方案?我还想动态添加受众(以支持多租户,这样我就可以动态创建消费者)@Verthosa我发现的解决方案是以不同的方式理解受众。我现在将“受众”理解为使用授权服务器的任何API或应用程序,而不是指调用这些API的客户端。因此,我可能有5个Api,它们都使用相同的授权服务器。每个api可能有数百个用户,但我只有5个观众(每个api 1个)。我相信这符合公约对RFC的思考。