c# - Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 警告 : 0 : invalid bearer token received

标签 c# asp.net-web-api oauth-2.0 jwt asp.net-identity

每当用户尝试访问 protected api 端点时,我都会收到此警告。身份验证工作正常,用户似乎已通过身份验证,但我一直无法弄清楚为什么此错误不断发生。我在没有启用 https/ssl 的 localhost 开发服务器上运行它。我可能错过了一个不太确定的步骤。我使用自定义提供程序实现了 token 身份验证和刷新 token 。然后实现自定义 jwt 格式来生成 token 。

This is the warning in the Application Output Log:

Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware Warning: 0 : invalid bearer token received

SimpleJWTFormat.cs:指定 jwt token 的格式

public class SimpleJwtFormat : ISecureDataFormat<AuthenticationTicket>
{

    private readonly string _issuer = string.Empty;

    public SimpleJwtFormat(string issuer)
    {
        _issuer = issuer;
    }

    public string Protect(AuthenticationTicket data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];

        string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
        var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);

        //var signingKey = new HmacSigningCredentials(keyByteArray);
        var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(keyByteArray);
        securityKey.KeyId = ConfigurationManager.AppSettings["as:AudienceId"];

        var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

        var issued = data.Properties.IssuedUtc;

        var expires = data.Properties.ExpiresUtc;

        var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingCredentials);
        //token.SigningKey = securityKey;

        var handler = new JwtSecurityTokenHandler();

        var jwt = handler.WriteToken(token);
        return jwt;
    }

    public AuthenticationTicket Unprotect(string protectedText)
    {
        string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
        string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];

        var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);
        var signingKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(keyByteArray); 

        var tokenValidationParameters = new TokenValidationParameters
        {
            ValidAudience = audienceId,
            ValidIssuer = _issuer,
            IssuerSigningKey = signingKey,
            ValidateLifetime = true,
            ValidateAudience = true,
            ValidateIssuer = true,
            RequireSignedTokens = true,
            RequireExpirationTime = true,
            ValidateIssuerSigningKey = true
        };


        var handler = new JwtSecurityTokenHandler();
        SecurityToken token = null;

        // Unpack token
        var pt = handler.ReadToken(protectedText);
        string t = ((JwtSecurityToken)pt).RawData;

        var principal = handler.ValidateToken(t, tokenValidationParameters, out token);
        var identity = principal.Identities;

        return new AuthenticationTicket(identity.First(), new AuthenticationProperties());
    }
}

最佳答案

问题是我没有在 Startup.cs 文件中调用 app.UseOAuthBearerAuthentication 并指定自定义 token 格式。

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
  AccessTokenFormat = _tokenFormat
});

关于c# - Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 警告 : 0 : invalid bearer token received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54826249/

相关文章:

oauth-2.0 - 访问 token JWT 验证

php - Google API OAuth 2.0 身份验证返回 "invalid_grant"- 可能是私钥签名格式问题?

c# - 获取方法的集合

c# - 在 C# 中运行 F# 代码 -(就像在 C# 中使用 Roslyn 的 C#)

c# - 使用 SimpleInjector 为 AccountController/ApplicationUserManager 注册 UserStore

python - Oauth2 身份验证 token 包含 Azure 应用程序 ID 的 OID,而不是用户 ID

c# - 如何获取这些 xml 值的字符串数组?

c# - 在 SQL Server 中比较字符串

angular - 如何将 Angular 4 Web 应用程序连接到移动摄像头?

javascript - 如何在 WEB.API 中显示自定义错误 JSON 结果