【问题标题】:"invalid bearer token received" when using newly requested bearer token使用新请求的不记名令牌时“收到无效的不记名令牌”
【发布时间】:2026-01-17 16:55:01
【问题描述】:

我正在使用 Postman,首先我请求一个令牌:

https://my-internal-test-site/identity/connect/token

这为我提供了正确的令牌响应:

{
    "access_token": "MY_BEARER_TOKEN",
    "expires_in": ?,
    "token_type": "Bearer",
    "refresh_token": "MY_REFRESH_TOKEN"
}

比我发布到我需要的实际网址:

https://my-internal-test-site/api/start-stuff

将此值用于授权标头:

BEARER *MY_BEARER_TOKEN*

虽然这是一个新请求的令牌,但我得到:

{
    "message": "Authorization has been denied for this request."
}

当我检查我的 API 日志时,我看到了:

Error | IdentityServer3.AccessTokenValidation.IntrospectionEndpointTokenProvider | Error returned from introspection endpoint: An error occurred while sending the request.

Warn | Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware | invalid bearer token received

这很奇怪,因为它是我正在使用的新令牌。可能是其他原因导致了这种情况,但如果是这样,这里没有描述,身份服务器日志中也没有描述。这似乎很好:

Info | IdentityServer3.Core.ResponseHandling.TokenResponseGenerator | Creating token response
Info | IdentityServer3.Core.ResponseHandling.TokenResponseGenerator | Processing token request
Info | Identity.Api.Monitoring.IdentityEventService | Information - 2020 - Refresh token issued -  - IdentityServer3.Core.Events.RefreshTokenDetails
Info | Identity.Api.Monitoring.IdentityEventService | Information - 2000 - Access token issued -  - IdentityServer3.Core.Events.AccessTokenIssuedDetails
Info | Identity.Api.Monitoring.IdentityEventService | Success - 3000 - Endpoint success -  - IdentityServer3.Core.Events.EndpointDetail
Info | IdentityServer3.Core.Endpoints.TokenEndpointController | End token request
Info | IdentityServer3.Core.Results.TokenResult | Returning token response.

知道会发生什么吗?

【问题讨论】:

  • 请向我们展示一个不起作用的令牌示例(真实值 - 不要虚构)。
  • 遇到了同样的问题,还没有找到解决办法。我从 api 得到 401,NLog 显示:Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware|invalid Bearer token received。我生成的令牌是 JWT Bearer 令牌。

标签: c# oauth-2.0 asp.net-web-api2 identityserver3


【解决方案1】:

数字,我得到的错误不是正在发生的错误:

底层 api url 不可公开访问

这就是为什么它在本地工作但不能在测试服务器上工作的原因,因为那时我正在使用该公共 url。奇怪的是,这些都没有出现在任何日志中,但是你能做什么......

我咨询了系统管理员,他们修复了它。

【讨论】:

    【解决方案2】:

    我在 localhost 上遇到了完全相同的错误消息,但测试服务器工作正常。

    事实证明,使用几个月前我开始使用的自签名 SSL 证书存在问题。 默认的 IIS express 开发证书运行良好,但我的自签名 localhost 证书(带有一些额外的 SAN 值)没有,直到我将它添加到本地计算机/受信任的根 CA。
    它已经添加到当前用户/受信任的根 CA 和本地计算机/受信任的人,但这还不够。

    【讨论】: