【发布时间】:2020-05-01 23:08:15
【问题描述】:
使用 Microsoft.Owin.Security.Jwt,您可以执行以下操作:
public static void ConfigureOAuth(IAppBuilder app)
{
OAuthConfiguration oAuthConfiguration = OAuthConfiguration.GetConfig("oauth");
List<string> audiences = new List<string>();
List<byte[]> secrets = new List<byte[]>();
foreach (var oAuthAudienceElement in /*configuration*/)
{
audiences.Add(/*configuration thingy*/);
secrets.Add(TextEncodings.Base64Url.Decode(/*configuration thingy*/));
}
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new List<string>(audiences),
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(oAuthConfiguration.Issuer.Domain, secrets)
}
});
}
但我无法在 ASP.NET Core 2.X 中找到与之等效的版本。这是不支持还是我错过了什么?我的意思是,services.AddJwtBearer 提供的不多:
services.AddAuthentication("oauth")
.AddOAuth("oauth", options =>
{
// something?
})
.AddJwtBearer("oauth", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
// These don't exist as in the Microsoft.Owin.Security.Jwt example above...
// AuthenticationMode = AuthenticationMode.Active,
// AllowedAudiences = new List<string>(audiences),
// IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
// {
// new SymmetricKeyIssuerSecurityTokenProvider(oAuthConfiguration.Issuer.Domain, secrets)
// }
};
});
【问题讨论】:
-
嗯,肯定是
AddJwtBearer接受Bearer 令牌。TokenValidationParameters上有一个ValidAudiences属性供观众使用。您应该也可以在那里设置对称签名密钥。 -
@juunas 我确实看到
ValidAudiences在那里。至于 .NET 中的IssuerSecurityTokenProviders,您的意思是它在 .NET Core 中的等价物是IssuerSigningKeys?我的意思是,一个是“提供者”,另一个不是。遗憾的是,这方面似乎没有很多资源。
标签: rest asp.net-core jwt