【发布时间】:2020-06-20 22:26:07
【问题描述】:
我有使用 OpenIddict 3.0 的不记名令牌身份验证。当客户端访问缺少令牌的授权控制器时,我希望它返回错误代码 401 Unauthorized not 400 Bad Request。
这是错误消息的来源,但 http 状态代码来自哪里,我将如何覆盖它?
OpenIddictValidationHandlers.cs
public ValueTask HandleAsync([NotNull] ProcessAuthenticationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (string.IsNullOrEmpty(context.Request.AccessToken))
{
context.Logger.LogError("The request was rejected because the access token was missing.");
context.Reject(
error: Errors.InvalidRequest,
description: "The access token is missing.");
return default;
}
context.Token = context.Request.AccessToken;
return default;
}
还有我的 Startup.cs
...
var openId = services.AddOpenIddict()
...
.AddValidation(config =>
{
config.UseLocalServer();
config.UseAspNetCore();
});
【问题讨论】: