验证Live.com(Microsoft帐户)JWT令牌

Mic*_*dej 8 c# security jwt owin asp.net-web-api2

研究员,

我目前正在努力使用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)
                            }
            });
    }
Run Code Online (Sandbox Code Playgroud)

我在这里发现了这个片段:

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 Error: 0 : Authentication failed
System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier
    (
    IsReadOnly = False,
    Count = 1,
    Clause[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause
    )
', 
token: '{"alg":"HS256","kid":"0","typ":"JWT"}.{"ver":1,"iss":"urn:windows:liveid","exp":1408666611,"uid":"my Microsoft account uid","aud":"(My API's domain)","urn:microsoft:appuri":"ms-app://(client app store id)","urn:microsoft:appid":"(ID of the app from account.live.com/developers)"}
RawData: (the JWT token)'.
   w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)
   w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
   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()

对不起我的英文,任何更正都非常欢迎.

Aym*_*med 0

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

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

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

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

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