如何将自定义数据传递给 HandleChallengeAsync

How to pass custom data to HandleChallengeAsync

我有一个 Web API,其中包含读取身份验证令牌的自定义身份验证方案。由于多种原因(令牌丢失、无效、过期),身份验证可能会失败 (401),因此我希望能够在 HTTP 响应中指示失败原因。例如,401 Token has expired.

令牌在 AuthenticationHandler<T>.HandleAuthenticateAsync 中被解析和验证。如何将失败原因从该方法传递给 HandleChallengeAsync

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
  AuthenticateResult result = null;
  var tokenResult = this.ParseToken(this.Request, out var token);

  if (tokenResult == TokenResult.Normal)
  {
    result = AuthenticateResult.Success(this.ticketFactory.CreateAuthenticationTicket(token));
  }
  else
  {
    result = AuthenticateResult.Fail("Bad token");
    // FIXME: Figure out how to populate the Properties property
    //result.Properties.Items.Add(nameof(TokenResult), tokenResult.ToString());
  }

  return Task.FromResult(result);
}

AuthenticationProperties.Items 看起来是存储此类自定义数据的好地方。但我不知道如何在 HandleAuthenticateAsync 中创建一个 AuthenticationProperties 对象并将其附加到 AuthenticateResult 对象。有办法吗?

身份验证处理程序是瞬态范围的,如您在此处的源代码中所见:https://github.com/dotnet/aspnetcore/blob/4dd2a883a4b22cf3df5944b1224355a94158f516/src/Security/Authentication/Core/src/AuthenticationBuilder.cs#L48

每个请求都会得到自己的处理程序实例,因此您可以将失败原因设置为实例字段。