Docusign JWT 身份验证 dotnet 核心问题

Docusign JWT authentication dotnet core issue

我需要使用 JWT Grant.In 在 docusign 中获取访问令牌,我对签名部分感到困惑。 这是我做的方式 第一个创建的代币

 var token = new JwtBuilder()
                         .WithAlgorithm(new HMACSHA256Algorithm())
                         .WithSecret(privateKey)
                         .AddClaim("iss", clientId)
                         .AddClaim("sub", accountId)
                         .AddClaim("iat", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
                         .AddClaim("aud", aud)
                         .AddClaim("scope", "signature impersonation")
                         .Encode();

这里添加了docusign的PrivateKey作为Secret。

这是邮递员请求

url: https://account-d.docusign.com/oauth/token
body: {
    "grant_type":"urn:ietf:params:oauth:grant-type:jwt-bearer",
    "assertion":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI4YzNkMDM0My01ZGQzLTQ3YjQtOWFkZi1iNjYxNTc4NjRmOWYiLCJzdWIiOiIwOWRjMWE4OC03Zjk1LTQ3Y2YtYTJkOC00NzQxYTc0N2E2NzUiLCJpYXQiOjE2MDM4Nzg3OTIsImF1ZCI6ImFjY291bnQtZC5kb2N1c2lnbi5jb20iLCJzY29wZSI6InNpZ25hdHVyZSBpbXBlcnNvbmF0aW9uIn0.QmbBst9_QOW9vZlI1CnVaUmv3AlNeWItRnPNiCE7vZ4"
}
method: post

它返回错误 request.Here 是错误信息

{
    "error": "invalid_grant",
    "error_description": "no_valid_keys_or_signatures"
}

请帮我解决这个问题

我强烈建议您考虑使用我们的 QuickStart,它将为您设置一切并使用 C# SDK。它已经是 .NET Core。 在你 运行 它之后,它是第一个授权码授予。 要更改为 JWT,您可以将 quickstart="true" 的配置修改为 "false",或者您可以注销并重新使用 JWT。

我已经解决了这个问题 问题方式 JWT 令牌签名不是有效的 algorithm.And 我必须添加 docusign 应用私钥才能添加。 我将私钥存储在一个名为 private.key 的文件中 这是代码

 var privateKeyfilePath = Path.Combine(Directory.GetCurrentDirectory(), "SignDocuments", "private.key");
            string privateKey = System.IO.File.ReadAllText(privateKeyfilePath);
            var rsaPrivate = RSA.Create();
            rsaPrivate.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out _);
            var privateKeyRSA = new RsaSecurityKey(rsaPrivate);
            SigningCredentials signingCredentials = new SigningCredentials(privateKeyRSA, SecurityAlgorithms.RsaSha256);

            var claims = new Claim[]
           {
                new Claim("iss", clientId),
                new Claim("sub", APIUsername),
                new Claim("iat", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds()+ ""),
                new Claim("aud", aud),
                new Claim("scope", "signature impersonation"),
           };

            var jwt = new JwtSecurityToken(
                signingCredentials: signingCredentials,
                claims: claims,
                notBefore: DateTime.UtcNow,
                expires: DateTime.UtcNow.AddHours(1),
                issuer: clientId
                );

之后我们必须获得内部应用程序的同意。 为此,请使用此 url。用你的

替换 clientId

记得在您使用 docusign 创建的应用中添加相同的重定向 url。 url 范围需要是 scope=signature%20impersonation(签名和模拟都需要)

在那之后 docusign 要求接受它。

您可以在关联的应用程序下的 docusign 管理配置文件中检查您的应用程序是否已获得同意

终于可以拿到access token了