Introduction
In this article, I will demonstrate how to send a document for signature from ASP.NET MVC5 web application using DocuSign. Please take a look at this DocuSign Part-1 article for basic account creation, integration, and validation with our application. Let's move forward to do a signing request.
Prerequisites
- Visual Studio
- Basic Knowledge of ASP.NET MVC
- Basic Knowledge of C#
- Should have an account in DocuSign
Article Flow
- Create an ASP.NET MVC Empty project
- Create a Controller and Design a View
- Integrate DocuSign
- Send the Document for a Sign
- Validate in DocuSign and Email
Create an ASP.NET MVC Empty project
- To create an ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2017.
- Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "DocusignDemo".
- Now, click OK.
- Then, select Empty ASP.NET MVC template and click OK to create the project.
- Once you click OK, the project will be created with the basic architecture of MVC. If you are not aware of how to create an Empty ASP.NET Web Application, please visit Step 1 and Step 2
Once you complete these steps, you will get the screen as below.
Create a Controller and Design a View
Now, create an empty Controller and View. Here, I have created a Controller with the name of "DocusignController". Whenever we create an empty Controller, it is created with an empty Index action method. And create an empty View of this action method "Index". Here we will create an action with the name of "SendDocumentforSign". And create a model to create a strongly typed view.
- public partial class Recipient {
- public string Name {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- public string Description {
- get;
- set;
- }
- }
Now, create a strongly typed view with this model. I have created the view as below,

To get the same design and control paste the below code in your view.
- @model DocusignDemo.Models.Recipient
- @{
- /**/
- ViewBag.Title = "SendDocumentforSign";
- }
- @using (Html.BeginForm("SendDocumentforSign", "Docusign", FormMethod.Post, new { enctype = "multipart/form-data", id="SendForsign" }))
- {
- @Html.AntiForgeryToken()
- <br />
- <div class="panel panel-primary col-md-6">
- <div class="panel-heading">Send For Sign</div>
- <div class="panel-body" >
- <div class="form-horizontal">
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.Label("Document", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- <input id="UploadDocument" type="file" name="UploadDocument" class="form-control" />
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Send For Sign" class="btn btn-primary" />
- </div>
- </div>
- </div>
- </div>
- </div>
- }
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.validate.min.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Integrate DocuSign
We have seen the DocuSign integration in my previous article with DocuSign.Integrations.Client.dll. But it's not comfortable for a beginner so I will go with another NuGet package for easier understanding, and there are no major changes in the code. Okay, let's take a look and install Docusign.eSign.dll from the NuGet.
Send the Document for a Sign
Now, create a post action in our controller as below to receive the data from view and to send the document to sign.
- [HttpPost]
- public ActionResult SendDocumentforSign(DocusignDemo.Models.Recipient recipient, HttpPostedFileBase UploadDocument)
- {
- }
Create a Folder Dynamically in our code to store the sending documents
- string directorypath = Server.MapPath("~/App_Data/" + "Files/");
- if (!Directory.Exists(directorypath))
- {
- Directory.CreateDirectory(directorypath);
- }
Here, we will the namespace as below,
- using DocuSign.eSign.Api;
- using DocuSign.eSign.Client;
- using DocuSign.eSign.Model;
- using Document = DocuSign.eSign.Model.Document;
Now, save the sending document in our server. Here we are working with pdf files so we need to convert every file(s) to pdf format.
- byte[] data;
- using(Stream inputStream = UploadDocument.InputStream) {
- MemoryStream memoryStream = inputStream as MemoryStream;
- if (memoryStream == null) {
- memoryStream = new MemoryStream();
- inputStream.CopyTo(memoryStream);
- }
- data = memoryStream.ToArray();
- }
- var serverpath = directorypath + recipient.Name.Trim() + ".pdf";
- System.IO.File.WriteAllBytes(serverpath, data);
Now, we need to send your document to the respective person, but before that we need to validate our DocuSign connection.
- public void docusign(string path, string recipientName, string recipientEmail) {
- ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
- Configuration.Default.ApiClient = apiClient;
- //Verify Account Details
- string accountId = loginApi(credential.UserName, credential.Password);
- }
- public string loginApi(string usr, string pwd) {
- // we set the api client in global config when we configured the client
- ApiClient apiClient = Configuration.Default.ApiClient;
- string authHeader = "{\"Username\":\"" + usr + "\", \"Password\":\"" + pwd + "\", \"IntegratorKey\":\"" + INTEGRATOR_KEY + "\"}";
- Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);
- // we will retrieve this from the login() results
- string accountId = null;
- // the authentication api uses the apiClient (and X-DocuSign-Authentication header) that are set in Configuration object
- AuthenticationApi authApi = new AuthenticationApi();
- LoginInformation loginInfo = authApi.Login();
- // find the default account for this user
- foreach(DocuSign.eSign.Model.LoginAccount loginAcct in loginInfo.LoginAccounts) {
- if (loginAcct.IsDefault == "true") {
- accountId = loginAcct.AccountId;
- break;
- }
- }
- if (accountId == null) { // if no default found set to first account
- accountId = loginInfo.LoginAccounts[0].AccountId;
- }
- return accountId;
- }
- after validating connection, convert the document as a byte
- byte[] fileBytes = System.IO.File.ReadAllBytes(path);
Now, we will append this to docuSign document, and for that we will use the predefined EnvelopeDefinition class. We can send one or more document at a time.
- EnvelopeDefinition envDef = new EnvelopeDefinition();
- envDef.EmailSubject = "Please sign this doc";
- // Add a document to the envelope
- Document doc = new Document();
- doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
- doc.Name = Path.GetFileName(path);
- doc.DocumentId = "1";
- envDef.Documents = new List<Document>();
- envDef.Documents.Add(doc);
Now, add a recipient(s) to sign the document. Here, also we can define more than one signer.
- // Add a recipient to sign the documeent
- DocuSign.eSign.Model.Signer signer = new DocuSign.eSign.Model.Signer();
- signer.Email = recipientEmail;
- signer.Name = recipientName;
- signer.RecipientId = "1";
- envDef.Recipients = new DocuSign.eSign.Model.Recipients();
- envDef.Recipients.Signers = new List<DocuSign.eSign.Model.Signer>();
- envDef.Recipients.Signers.Add(signer);
- set envelope status to "sent" to immediately send the signature request
- envDef.Status = "sent";
- EnvelopesApi contains methods related to creating and sending Envelopes (ask a signature requests)
- EnvelopesApi envelopesApi = new EnvelopesApi();
- EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
To Get an envelopeSummary we need to use a JsonConvert. So install it from the NuGet.
Now, use JsonConvert to seraializeObject.
- var result = JsonConvert.SerializeObject(envelopeSummary);
Now, run your application.

In the above image, you can see that we successfully received the envelope summary and we might get a distinct envelope id for each DocuSign sign request.
Validate in DocuSign and Email
Now we shall check the DocuSign portal and email to see whether we have received any document. First, as an admin, we will check in DocuSign portal. In the below image, you can see the sent document with status, DateTime and recipient name.
Now as a user, we will check the mailbox. In the below image, you can see that we have received the mail from DocuSign to sign.

Now, the user is able to sign/review the document by clicking the mailed link. In the next article we will see how to track whether the user signed or viewed in our web application.
Complete Controller
- using DocuSign.eSign.Api;
- using DocuSign.eSign.Client;
- using DocuSign.eSign.Model;
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using System.IO;
- using System.Web;
- using System.Web.Mvc;
- using Document = DocuSign.eSign.Model.Document;
- namespace DocusignDemo.Controllers {
- public class DocusignController: Controller {
- MyCredential credential = new MyCredential();
- private string INTEGRATOR_KEY = "Enter Integrator Key";
- public ActionResult SendDocumentforSign() {
- return View();
- }
- [HttpPost]
- public ActionResult SendDocumentforSign(DocusignDemo.Models.Recipient recipient, HttpPostedFileBase UploadDocument) {
- Models.Recipient recipientModel = new Models.Recipient();
- string directorypath = Server.MapPath("~/App_Data/" + "Files/");
- if (!Directory.Exists(directorypath)) {
- Directory.CreateDirectory(directorypath);
- }
- byte[] data;
- using(Stream inputStream = UploadDocument.InputStream) {
- MemoryStream memoryStream = inputStream as MemoryStream;
- if (memoryStream == null) {
- memoryStream = new MemoryStream();
- inputStream.CopyTo(memoryStream);
- }
- data = memoryStream.ToArray();
- }
- var serverpath = directorypath + recipient.Name.Trim() + ".pdf";
- System.IO.File.WriteAllBytes(serverpath, data);
- docusign(serverpath, recipient.Name, recipient.Email);
- return View();
- }
- public string loginApi(string usr, string pwd) {
- // we set the api client in global config when we configured the client
- ApiClient apiClient = Configuration.Default.ApiClient;
- string authHeader = "{\"Username\":\"" + usr + "\", \"Password\":\"" + pwd + "\", \"IntegratorKey\":\"" + INTEGRATOR_KEY + "\"}";
- Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);
- // we will retrieve this from the login() results
- string accountId = null;
- // the authentication api uses the apiClient (and X-DocuSign-Authentication header) that are set in Configuration object
- AuthenticationApi authApi = new AuthenticationApi();
- LoginInformation loginInfo = authApi.Login();
- // find the default account for this user
- foreach(DocuSign.eSign.Model.LoginAccount loginAcct in loginInfo.LoginAccounts) {
- if (loginAcct.IsDefault == "true") {
- accountId = loginAcct.AccountId;
- break;
- }
- }
- if (accountId == null) { // if no default found set to first account
- accountId = loginInfo.LoginAccounts[0].AccountId;
- }
- return accountId;
- }
- public void docusign(string path, string recipientName, string recipientEmail) {
- ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
- Configuration.Default.ApiClient = apiClient;
- //Verify Account Details
- string accountId = loginApi(credential.UserName, credential.Password);
- // Read a file from disk to use as a document.
- byte[] fileBytes = System.IO.File.ReadAllBytes(path);
- EnvelopeDefinition envDef = new EnvelopeDefinition();
- envDef.EmailSubject = "Please sign this doc";
- // Add a document to the envelope
- Document doc = new Document();
- doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
- doc.Name = Path.GetFileName(path);
- doc.DocumentId = "1";
- envDef.Documents = new List < Document > ();
- envDef.Documents.Add(doc);
- // Add a recipient to sign the documeent
- DocuSign.eSign.Model.Signer signer = new DocuSign.eSign.Model.Signer();
- signer.Email = recipientEmail;
- signer.Name = recipientName;
- signer.RecipientId = "1";
- envDef.Recipients = new DocuSign.eSign.Model.Recipients();
- envDef.Recipients.Signers = new List < DocuSign.eSign.Model.Signer > ();
- envDef.Recipients.Signers.Add(signer);
- //set envelope status to "sent" to immediately send the signature request
- envDef.Status = "sent";
- // |EnvelopesApi| contains methods related to creating and sending Envelopes (aka signature requests)
- EnvelopesApi envelopesApi = new EnvelopesApi();
- EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
- // print the JSON response
- var result = JsonConvert.SerializeObject(envelopeSummary);
- }
- }
- public class MyCredential {
- public string UserName {
- get;
- set;
- } = "Enter UserName";
- public string Password {
- get;
- set;
- } = "Enter Password";
- }
- }
View
- @model DocusignDemo.Models.Recipient
- @{
- /**/
- ViewBag.Title = "SendDocumentforSign";
- }
- @using (Html.BeginForm("SendDocumentforSign", "Docusign", FormMethod.Post, new { enctype = "multipart/form-data", id="SendForsign" }))
- {
- @Html.AntiForgeryToken()
- <br />
- <div class="panel panel-primary col-md-6">
- <div class="panel-heading">Send For Sign</div>
- <div class="panel-body" >
- <div class="form-horizontal">
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.Label("Document", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- <input id="UploadDocument" type="file" name="UploadDocument" class="form-control" />
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Send For Sign" class="btn btn-primary" />
- </div>
- </div>
- </div>
- </div>
- </div>
- }
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.validate.min.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace DocusignDemo.Models {
- public partial class Recipient {
- public string Name {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- public string Description {
- get;
- set;
- }
- }
- }
Refer to the attached project for reference, and I did attach the demonstrated project without package due to the size limit.
Summary
In this article, we have discussed how to send a document for signature from ASP.NET MVC5 web application using DocuSign. I hope it will help you out. Your valuable feedback and comments about this article are always welcome.

Moosa SahibPosted Aug 14, 2024, 4:01 AM
Got this error using my docusign credentials. Its works with your credentials. DocuSign.eSign.Client.ApiException: 'Error calling CreateEnvelope: { "errorCode": "USER_AUTHENTICATION_FAILED", "message": "One or both of Username and Password are invalid." }'
Alma LaRoccoPosted Sep 19, 2023, 2:59 PM
I would really appreciate if you answer the authentication failed error questions. Thanks for sharing.
Jivan SunklodPosted Apr 12, 2023, 1:00 PM
Please respond to this issue : I have found the the Error calling CreateEnvelope: { "errorCode": "USER_AUTHENTICATION_FAILED", "message": "One or both of Username and Password are invalid." }
Lavanya LavsPosted Oct 21, 2022, 2:37 AM
Please respond for below issue
Lavanya LavsPosted Oct 21, 2022, 2:28 AM
Gnanavel Sekar Please respond to this issue : I have found the the Error calling CreateEnvelope: { "errorCode": "USER_AUTHENTICATION_FAILED", "message": "One or both of Username and Password are invalid." }
Anil JambukiyaPosted Aug 26, 2022, 2:03 PM
I have found the the Error calling CreateEnvelope: { "errorCode": "USER_AUTHENTICATION_FAILED", "message": "One or both of Username and Password are invalid." }
Rob MartinPosted Jul 8, 2021, 5:44 AM
" In the next article we will see how to track whether the user signed or viewed in our web application." - Do you already have this next article?
kavin venkatPosted Mar 19, 2021, 9:37 PM
In your example , apiclien url i see the "https://demo.docusign.net/restapi" . Is this url from docusign or you created your own restapi? . Please advise me. 2) Is it possible we can embed the sign in the html page?
bachi bhaskarPosted Nov 23, 2020, 3:47 PM
Hi, Here's my code below in vb.net. I am getting error :Non-Protocol Error: SecureChannelFailure ". Can you please provide any suggestions. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load RestSettings.Instance.DocuSignAddress = "https://demo.docusign.net" RestSettings.Instance.WebServiceUrl = RestSettings.Instance.DocuSignAddress + "/restapi/V2" RestSettings.Instance.IntegratorKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 'Mention your created Integrator key Dim Account As New DocuSign.Integrations.Client.Account Account.Email = "xxxxxxxxx" 'Mention your Account Username Account.Password = "xxxxxx" 'Mention your Account Password Dim result As Boolean = Account.Login() If result Then Console.WriteLine("Docusign Integration Success") Else Console.WriteLine("Docusign Integration Failed") lblErrMessage.Text = Account.RestError.message End If End Sub End Class
Manish AgrawalPosted Nov 8, 2020, 1:08 PM
Thanks for a very useful tutorial. I am getting the following error for Configuration.Default.ApiClient = apiClient; "Severity Code Description Project File Line Suppression StateError CS1061 'Configuration' does not contain a definition for 'ApiClient' and no accessible extension method 'ApiClient' accepting a first argument of type 'Configuration' could be found (are you missing a using directive or an assembly reference?)" I am using MVC Core
raja sekarPosted Jun 24, 2020, 7:39 AM
8524947945
raja sekarPosted Jun 24, 2020, 7:38 AM
This is very good article ji..but initiator should be decide the location for where need to sign the document.
saisudha kolimiPosted Jun 13, 2020, 11:15 AM
This is the error message.. I am not sure how to proceed. Can you please help me on this. Error calling CreateEnvelope: { "errorCode": "PDF_VALIDATION_FAILED", "message": "The validation of the PDF file failed." }
saisudha kolimiPosted Jun 13, 2020, 11:14 AM
Hi Sekar, thanks for the tutorial. I am receiving the PDF validation error while trying to execute create envelope method. EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
Bryian TanPosted Nov 6, 2018, 7:34 AM
Good stuff..
Humayun Kabir MamunPosted Nov 5, 2018, 8:39 PM
Thanks for this nice article