【问题标题】:How to sign JWT with a RSA SHA-256 hash如何使用 RSA SHA-256 哈希对 JWT 进行签名
【发布时间】:2016-08-01 18:13:27
【问题描述】:

我正在尝试通过客户端凭据获取访问令牌以使用 office365 api。我正在使用本指南:Office 365 Rest API - Daemon week authentication

我正在使用邮递员发送我的请求(见下文) Postman Picture

但是,当我发送请求“AADSTS70002:验证凭据时出错。AADSTS50012:客户端断言包含无效签名”时,邮递员给了我这个错误

所以我相当肯定我没有正确签署用于我请求中的 client_assertion 参数的 JWT。参考这个堆栈溢出问题Could not retrieve app only tokens for office 365 我发现我需要使用 RSA SHA-256 哈希对其进行签名。但是,我仍然无法让我的 JWT 使用我在网上找到的有关如何执行此操作的任何资源,它仍然会返回相同的错误。是否有一个在线生成器可以用来使用 RSA SHA-256 哈希对我的 JWT 进行签名?或者任何专门在 C# 中以这种方式唱歌的代码示例?提前致谢。

【问题讨论】:

    标签: oauth-2.0 jwt postman office365api rsa-sha256


    【解决方案1】:

    首先,您需要在 Azure AD 清单上设置证书,请参阅博客 Building Daemon or Service Apps with Office 365 Mail, Calendar, and Contacts APIs (OAuth2 client credential flow)

    关于如何签署令牌,这里是C#示例供您参考,

        var x509Certificate2 = new X509Certificate2(@"{FILE PATH}\office_365_app.pfx", "PASS_WORD");
    
        X509SigningCredentials signingCredentials = new X509SigningCredentials(x509Certificate2, SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);
    
        JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
    
        var originalIssuer = "{YOUR CLIENT ID}";
    
        var issuer = originalIssuer;
    
        DateTime utcNow = DateTime.UtcNow;
    
        DateTime expired = utcNow + TimeSpan.FromHours(1);
    
        var claims = new List<Claim> {
            new Claim("aud", "https://login.microsoftonline.com/{YOUR_TENENT_ID}/oauth2/token", ClaimValueTypes.String, issuer, originalIssuer),
            new Claim("exp", "1460534173", ClaimValueTypes.DateTime, issuer, originalIssuer), 
            new Claim("jti", "{SOME GUID YOU ASSIGN}", ClaimValueTypes.String, issuer, originalIssuer),
            new Claim("nbf", "1460533573", ClaimValueTypes.String, issuer, originalIssuer),
            new Claim("sub", "{YOUR CLIENT ID}", ClaimValueTypes.String, issuer, originalIssuer)
        };
    
        ClaimsIdentity subject = new ClaimsIdentity(claims: claims);
    
        JwtSecurityToken jwtToken = tokenHandler.CreateToken(
            issuer: issuer,
            signingCredentials: signingCredentials,
            subject: subject) as JwtSecurityToken;
    
        jwtToken.Header.Remove("typ");
    
        var token = tokenHandler.WriteToken(jwtToken);
    

    您也可以在 GitHub 上找到该项目

    https://github.com/dream-365/OfficeDev-Samples/blob/master/samples/Office365DevQuickStart/JWT-Token

    【讨论】:

      【解决方案2】:

      您还可以使用https://github.com/jwtk/jjwt 提供的 JJWT api 签署您的 JWT 令牌

      示例代码如下所示:

                         Map<String, Object> claims = new HashMap<>();
                         claims.put("user", "some user");
                         Calendar expires = Calendar.getInstance();
                         expires.roll(Calendar.HOUR, 1000);
                          Jwts.builder()
                              .setClaims(claims)
                              .setIssuedAt(new Date())
                              .setExpiration(expires.getTime())
                              .signWith(SignatureAlgorithm.RS256, key)
                              .compact();
      

      您还可以在JWT.io 上验证您的令牌

      【讨论】:

        猜你喜欢
        • 2021-02-04
        • 2021-04-03
        • 2014-05-13
        • 2018-02-22
        • 2014-08-19
        • 1970-01-01
        • 2021-10-29
        • 2023-03-18
        • 1970-01-01
        相关资源
        最近更新 更多