[英]In ASP.NET Web API Core, how can I force a 401 response
I am using OAuth2 and OIDC in my ASP.NET Core Web API Service.我在我的 ASP.NET Core Web API 服务中使用 OAuth2 和 OIDC。 When I get a JWT token in, it validates it.
当我收到 JWT 令牌时,它会对其进行验证。
In my case, my Issuer is not matching.就我而言,我的发行人不匹配。 I am able to see this because the results of the validation are surfaced in the
JwtBearerEvents
event OnAuthenticationFailed
.我能够看到这一点,因为验证结果显示在
JwtBearerEvents
事件OnAuthenticationFailed
。 That is the point where I can know that I need to return 401.这就是我可以知道我需要返回 401 的地方。
How can I set my service to return 401 at that point?我如何设置我的服务在那时返回 401?
I haven't had a chance to test this in a real project, but if you just want to return a 401, then you might want to try something like:我还没有机会在实际项目中对此进行测试,但是如果您只想返回 401,那么您可能想尝试以下操作:
OnAuthenticationFailed = context =>
{
context.Response.StatusCode = 401;
context.Response.WriteAsync("Unauthorized").Wait();
return Task.CompletedTask;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.