(我在.Net中遇到过类似的情况,所以在这种情况下)
不,如果您使用的是 oauth,则不必编写新的验证令牌方法。作为 OAuthBearerAuthenticationProvider 在幕后执行此操作
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
},
Provider = new OAuthBearerAuthenticationProvider
{
OnValidateIdentity = context =>
{
context.Ticket.Identity.AddClaim(new System.Security.Claims.Claim("newCustomClaim", "newValue"));
return Task.FromResult<object>(null);
}
}
});
(根据我的经验)。但是,如果您愿意,可以选择在“启动”文件中配置 Provider:
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
},
Provider = new CustomOAuthBearerProvider()
});
"CustomOAuthBearerProvider" 继承了 "IOAuthBearerAuthenticationProvider" 接口,该接口为 RequestToken() 方法预定义了签名,并且在对令牌进行任何验证之前调用此方法。因此,我认为您可以将其用于对 Token 的自定义验证操作,然后发送令牌进行 OAuth 验证。