【问题标题】:Validate Live.com (Microsoft account) JWT token验证 Live.com(Microsoft 帐户)JWT 令牌
【发布时间】:2014-10-15 13:17:48
【问题描述】:

各位程序员,

我目前正在为 Web Api 2 中的 Microsoft 帐户 JWT 令牌验证而苦苦挣扎。 我已经找到了 OWIN 中间件(NuGet 包 Microsoft.Owin.Security.Jwt),这是我的 Startup.cs 中配置的代码:

    public void ConfigureAuth(IAppBuilder app)
    {
        var sha256 = new SHA256Managed();
        var secretBytes = System.Text.Encoding.UTF8.GetBytes(@"(My app client secret)" + "JWTSig");
        byte[] signingKey = sha256.ComputeHash(secretBytes);

        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AllowedAudiences = new[] { "(My API's domain )" },
                IssuerSecurityTokenProviders =
                    new[]
                            {
                                new SymmetricKeyIssuerSecurityTokenProvider(
                                    "urn:windows:liveid", signingKey)
                            }
            });
    }

我在这里找到了 sn-p:

http://code.lawrab.com/2014/01/securing-webapi-with-live-id.html

JWT 令牌是使用 Live SDK 从我的 Windows 应用商店应用客户端发送的。我正在发送身份验证令牌,而不是访问令牌,所以我确定它是 JWT。使用像这样的在线调试器:http://jwt.io/ 我能够成功解码标头和有效负载部分,但我找不到验证签名的方法。发送带有该 JWT 的请求时,我的 Web API 的调试输出是:

Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 错误:0:身份验证失败 System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException:IDX10500:签名验证失败。无法解析 SecurityKeyIdentifier:'SecurityKeyIdentifier ( IsReadOnly = 假, 计数 = 1, 子句[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause ) ', 令牌:'{"alg":"HS256","kid":"0","typ":"JWT"}.{"ver":1,"iss":"urn:windows:liveid","exp ":1408666611,"uid":"我的 Microsoft 帐户 uid","aud":"(我的 API 的域)","urn:microsoft:appuri":"ms-app://(客户端应用商店 id)", "urn:microsoft:appid":"(来自 account.live.com/developers 的应用 ID)"} 原始数据:(JWT 令牌)'。 w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParameters 验证参数) w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(字符串安全令牌,令牌验证参数验证参数,安全令牌和验证令牌) w Microsoft.Owin.Security.Jwt.JwtFormat.Unprotect(String protectedText) w Microsoft.Owin.Security.Infrastructure.AuthenticationTokenReceiveContext.DeserializeTicket(String protectedData) w Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.d__0.MoveNext()

对不起我的英语,任何更正都非常受欢迎。

【问题讨论】:

标签: c# security owin asp.net-web-api2 jwt


【解决方案1】:

我可以使用的最简单的方法之一是从源本身验证它。

在您的情况下,您现在正在使用 live.com ,然后向 live.com 发送一个请求并在标头中使用您的令牌,如果它是有效的标头,它将返回已知值(例如用户帐户信息)

选择这样的网址: https://outlook.live.com/ows/v1.0/OutlookOptions

并将标头中的令牌作为 Authorization: Bearer TOKEN_VALUE 发送

如果它返回了预期值,那么它是一个有效的令牌并且会话也在工作

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 2021-03-04
    • 2018-11-21
    • 2019-10-03
    • 2019-06-23
    • 2018-02-10
    • 2017-07-27
    • 2019-10-20
    • 2016-01-17
    相关资源
    最近更新 更多