【发布时间】:2021-01-19 03:50:49
【问题描述】:
我有一些类似的代码:
[HttpGet]
[Authorize("MyCustomPolicy")]
public string TestCall()
{
var lanId = User.GetSomeClaimOnTheUserObject() ?? "Not Found";
return "Hello From a TestCall Get Operation. Claim is " + lanId;
}
授权策略如下所示:
var myCustomPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes("SomeScheme")
.Build();
options.AddPolicy("MyCustomPolicy", myCustomPolicy);
当我调用它时,如果 AuthenticationScheme 已经满足,它允许调用,如果没有满足则阻止它(如预期的那样)。
不起作用的部分是当它阻止调用时,我希望返回 403 或可能返回 401,但我得到的是返回 200(OK)。
当Authorize 属性失败时,如何让它返回 401?
【问题讨论】:
标签: security asp.net-core authorization asp.net-core-3.1