Skip to main content

I am integrating DocuSign into my ASP.NET Core application but facing an issue with creating a JWT token using an RSA private key. How can I generate a valid JWT token with the RSA private key?

@Alwinsurya  to generate JWT token using an RSA private key please proceed as follows:

 

1. Generate RSA key pair as instructed in the article below.

 

https://developers.docusign.com/platform/configure-app/

 

2. Grant consent to your application as explained in the article below

https://www.docusign.com/blog/developers/oauth-jwt-granting-consent

 

3. You the Quickstart to download a personalised project with code examples.

https://developers.docusign.com/docs/esign-rest-api/quickstart/

 

Alternatively tou can use the SDK below

https://github.com/docusign/docusign-esign-csharp-client

 

JWTAuth Class

 

// <copyright file="JWTAuth.cs" company="DocuSign">

// Copyright (c) DocuSign. All rights reserved.

// </copyright>

 

namespace DocuSign.CodeExamples.Authentication

{

using System;

using System.Collections.Generic;

using DocuSign.CodeExamples.Common;

using DocuSign.eSign.Client;

using static DocuSign.eSign.Client.Auth.OAuth;

 

public static class JwtAuth

{

/// <summary>

/// Uses Json Web Token (JWT) Authentication Method to obtain the necessary information needed to make API calls.

/// </summary>

/// <returns>Auth token needed for API calls</returns>

public static OAuthToken AuthenticateWithJwt(string api, string clientId, string impersonatedUserId, string authServer, byteS] privateKeyBytes)

{

var docuSignClient = new DocuSignClient();

var apiType = Enum.Parse<ExamplesApiType>(api);

var scopes = new List<string>

{

"signature",

"impersonation",

};

if (apiType == ExamplesApiType.Rooms)

{

scopes.AddRange(new List<string>

{

"dtr.rooms.read",

"dtr.rooms.write",

"dtr.documents.read",

"dtr.documents.write",

"dtr.profile.read",

"dtr.profile.write",

"dtr.company.read",

"dtr.company.write",

"room_forms",

});

}

 

if (apiType == ExamplesApiType.Click)

{

scopes.AddRange(new List<string>

{

"click.manage",

"click.send",

});

}

 

if (apiType == ExamplesApiType.Monitor)

{

scopes.AddRange(new List<string>

{

"signature",

"impersonation",

});

}

 

if (apiType == ExamplesApiType.Admin)

{

scopes.AddRange(new List<string>

{

"user_read",

"user_write",

"account_read",

"organization_read",

"group_read",

"permission_read",

"identity_provider_read",

"domain_read",

"user_data_redact",

"asset_group_account_read",

"asset_group_account_clone_write",

"asset_group_account_clone_read",

});

}

 

if (apiType == ExamplesApiType.WebForms)

{

scopes.Add("webforms_read");

scopes.Add("webforms_instance_write");

scopes.Add("webforms_instance_read");

}

 

return docuSignClient.RequestJWTUserToken(

clientId,

impersonatedUserId,

authServer,

privateKeyBytes,

1,

scopes);

}

}

}

 

Example:

 

You can use  the JWTAuth Class to generate the token as follows:

 

OAuthToken accessToken = JwtAuth.AuthenticateWithJwt("ESignature","INTEGRATION_KEY" ,

"USER_ID","account-d.docusign.com", BYTES_FROM_PrivateKeyFile");

  • Use account-d.docusign.com for development environment and account.docusign.com for production environment.
  • USER_ID is the userId who granted consent in step 2.
  • INTEGRATION_KEY can be found (In Docusign eSignature Settings, select Apps and Keys)

Reply