Author avatar

nescampos

Sign Contracts with ASP.NET MVC and HelloSign API

nescampos

  • Jan 10, 2019
  • 8 Min read
  • 7,616 Views
  • Jan 10, 2019
  • 8 Min read
  • 7,616 Views
Microsoft.NET

Introduction

This guide is designed to show you how to work with HelloSign API to send and sign documents via ASP.NET MVC.

HelloSign is a service that allows you to sign documents, and can be used to legalize agreements, preparing contracts, among others.

The main advantage for the user is the ability to perform the signature, either from his/her computer, tablet or smartphone.

This tutorial aims to show a simple example of using the API without major complexities, but the use that can be given is much broader.

For this tutorial, we will use:

  • ASP.NET MVC 4
  • HelloSign SDK (C#)

Register in HelloSign

We access https://www.hellosign.com/ and we create a free account.

Then we access the "API" section and get the API Key.

description

Now we create an application for the Client ID

description

Configure tools

First we need to create a project with Visual Studio (ideally version 2015). We selected a ASP.NET project (in this case we do not need larger configurations).

description

Then we install the HelloSign SDK through NuGet:

1Install-Package HelloSign
c#

Develop form to upload documents

We create a controller called BaseController, which we used to store the API Key and Client ID

1public class BaseController : Controller
2{
3    protected string HelloSignAPIKey = ConfigurationManager.AppSettings["HelloSignAPIKey"];
4    protected string HelloSignClientID = ConfigurationManager.AppSettings["HelloSignClientID"];
5}
c#

Then we create a controller called SignController, that inherits from our base controller. In our controller we will create a GET action called SendDocument:

1public ActionResult SendDocument()
2{
3    SendDocumentViewModel model = new SendDocumentViewModel();
4    return View(model);
5}
c#

We add the SendDocumentFormModel and SendDocumentViewModel classes to send and receive data from the view

1public class SendDocumentFormModel
2{
3    [Required]
4    public string Subject { get; set; }
5
6    [Required]
7    public string Message { get; set; }
8
9    [Required]
10    [DisplayName("Signer Email")]
11    public string SignerEmail { get; set; }
12
13    [Required]
14    [DisplayName("Signer Name")]
15    public string SignerName { get; set; }
16
17    [Required]
18    public HttpPostedFileBase File { get; set; }
19
20}
c#
1public class SendDocumentViewModel
2{
3    public SendDocumentFormModel Form { get; set; }
4}
c#

Now we create the POST action for SendDocument.

1[HttpPost]
2public ActionResult SendDocument(SendDocumentFormModel Form)
3{
4    if(!ModelState.IsValid)
5    {
6        SendDocumentViewModel model = new SendDocumentViewModel();
7        model.Form = Form;
8        return View(model);
9    }
10    var client = new Client(HelloSignAPIKey);
11    var request = new SignatureRequest();
12    request.Subject = Form.Subject;
13    request.Message = Form.Message;
14    request.AddSigner(Form.SignerEmail, Form.SignerName);
15    byte[] arreglo = new byte[Form.File.ContentLength];
16    Form.File.InputStream.Read(arreglo, 0, Form.File.ContentLength);
17    request.AddFile(arreglo, Form.File.FileName);
18    request.TestMode = true;
19    var response = client.CreateEmbeddedSignatureRequest(request, HelloSignClientID);
20    var urlSign = client.GetSignUrl(response.Signatures[0].SignatureId);
21    return RedirectToAction("Sign", new { url = urlSign.SignUrl });
22}
c#

The response to sending the document will be a URL with the embedded file ready to be signed.

To finish our form, we create the view to upload documents.

1@using (Html.BeginForm(null,null,FormMethod.Post,new {enctype="multipart/form-data" }))
2{
3
4    <div class="form-horizontal">
5        <hr />
6        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
7        <div class="form-group">
8            @Html.LabelFor(model => model.Form.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
9            <div class="col-md-10">
10                @Html.EditorFor(model => model.Form.Subject, new { htmlAttributes = new { @class = "form-control" } })
11                @Html.ValidationMessageFor(model => model.Form.Subject, "", new { @class = "text-danger" })
12            </div>
13        </div>
14
15        <div class="form-group">
16            @Html.LabelFor(model => model.Form.Message, htmlAttributes: new { @class = "control-label col-md-2" })
17            <div class="col-md-10">
18                @Html.EditorFor(model => model.Form.Message, new { htmlAttributes = new { @class = "form-control" } })
19                @Html.ValidationMessageFor(model => model.Form.Message, "", new { @class = "text-danger" })
20            </div>
21        </div>
22
23        <div class="form-group">
24            @Html.LabelFor(model => model.Form.SignerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
25            <div class="col-md-10">
26                @Html.EditorFor(model => model.Form.SignerEmail, new { htmlAttributes = new { @class = "form-control", type="email" } })
27                @Html.ValidationMessageFor(model => model.Form.SignerEmail, "", new { @class = "text-danger" })
28            </div>
29        </div>
30
31        <div class="form-group">
32            @Html.LabelFor(model => model.Form.SignerName, htmlAttributes: new { @class = "control-label col-md-2" })
33            <div class="col-md-10">
34                @Html.EditorFor(model => model.Form.SignerName, new { htmlAttributes = new { @class = "form-control" } })
35                @Html.ValidationMessageFor(model => model.Form.SignerName, "", new { @class = "text-danger" })
36            </div>
37        </div>
38        <div class="form-group">
39            @Html.LabelFor(model => model.Form.File, htmlAttributes: new { @class = "control-label col-md-2" })
40            <div class="col-md-10">
41                <input type="file" name="Form.File" class = "form-control" />
42                @Html.ValidationMessageFor(model => model.Form.File, "", new { @class = "text-danger" })
43            </div>
44        </div>
45
46        <div class="form-group">
47            <div class="col-md-offset-2 col-md-10">
48                <input type="submit" value="Create" class="btn btn-default" />
49            </div>
50        </div>
51    </div>
52}
cs

Our view should look similar to the following:

description

Develop view for signing documents

As always, we first create the action in our SignController:

1public ActionResult Sign(string url)
2{
3    ViewBag.Url = url;
4    ViewBag.ClientId = HelloSignClientID;
5    return View();
6}
c#

Now, our view "Sign".

1<p>Sign document.</p>
2
3
4<script type="text/javascript" src="https://s3.amazonaws.com/cdn.hellosign.com/public/js/hellosign-embedded.LATEST.min.js"></script>
5<script type="text/javascript">
6    HelloSign.init("@ViewBag.ClientId");
7    HelloSign.open({
8        url: '@ViewBag.Url',
9        allowCancel: true,
10        skipDomainVerification : false
11    });
12</script>
html

In this view we only add javascript code. The first line calls the latest version of HelloSign, and the second one initializes HelloSign and opens our document as an embedded file.

By uploading our document on the form, if everything is valid should display the following view with the document ready to be signed.

description

Finally we click on "Click to sign" and we sign and complete the process.

description

This example shows the basic usage of HelloSign SDK for C#, and it could be used to send emails with links to our app for signing documents, etc.