我想在OnCreatingTicket事件期间导致oAuth失败。我可以打电话给。但它似乎什么也没做。在工作流程中是否为时已晚?我是否遗漏了一些额外的代码来实际处理失败?
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie().AddOAuth("schemename", SetOAuthOptions);
private void SetOAuthOptions(OAuthOptions options)
{
options.ClientId = ...;
options.ClientSecret = ...;
options.CallbackPath = ...;
options.AuthorizationEndpoint = ...;
options.TokenEndpoint = ...;
options.Events = new OAuthEvents
{
OnCreatingTicket = async context => await AddIdentityClaimsAsync(context),
OnTicketReceived = ...
};
}
private async Task AddIdentityClaimsAsync(OAuthCreatingTicketContext context)
{
...
if(noAccess)
context.Fail("Sorry, you don't have access to the product.");
}
答案 0 :(得分:0)
这就是我最终的结果:
在启动时,在AddOAuth之后:
.AddScheme<OAuthOptions, BearerAuthHandler>(BearerAuthHandler.DefaultSchemeName, ...);
这在您的控制器中:
[Authorize(AuthenticationSchemes = BearerAuthHandler.DefaultSchemeName)]