/
Program.cs
189 lines (171 loc) · 7.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
// Dokobit WS API .NET C# Example - Mobile ID Signing
namespace DokobitDotnetExample
{
class Program
{
public class Api
{
public static string accessToken = ""; //Enter Your Dokobit WS API access token
}
[DataContract]
public class Response
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(IsRequired = false, Name = "message")]
public string Message { get; set; }
[DataMember(IsRequired = false, Name = "errors")]
public IEnumerable<string> Errors { get; set; }
}
[DataContract, KnownType(typeof(Response))]
public class RequestResponse : Response
{
[DataMember(IsRequired = false, Name = "control_code")]
public string ControlCode { get; set; }
[DataMember(IsRequired = false, Name = "token")]
public string Token { get; set; }
}
[DataContract, KnownType(typeof(Response))]
public class FileResponse : Response
{
[DataMember(IsRequired = false, Name = "signature_id")]
public string SignatureId { get; set; }
[DataMember(IsRequired = false, Name = "file")]
public File File { get; set; }
}
[DataContract]
public class File
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "content")]
public string Content { get; set; }
[DataMember(Name = "digest")]
public string Digest { get; set; }
}
public static RequestResponse Sign(byte[] document, string phone, string code)
{
using (var client = new HttpClient())
{
using (var content =
new MultipartFormDataContent("Upload----" + DateTime.Now))
{
// Signed document format. Check documentation for all available options.
content.Add(new StringContent("pdf"), "type");
content.Add(new StringContent(phone), "phone");
content.Add(new StringContent(code), "code");
content.Add(new StringContent("true"), "timestamp");
content.Add(new StringContent("Name Surname"), "pdf[contact]");
content.Add(new StringContent("Test"), "pdf[reason]");
content.Add(new StringContent("Vilnius"), "pdf[location]");
content.Add(new StringContent("test.pdf"), "pdf[files][0][name]");
content.Add(new StringContent(Convert.ToBase64String(document)), "pdf[files][0][content]");
content.Add(
new StringContent(
BitConverter.ToString(SHA256.Create().ComputeHash(document)).Replace("-", "").ToLower()),
"pdf[files][0][digest]");
using (
var message =
client.PostAsync("https://developers.dokobit.com/mobile/sign.json?access_token=" + Api.accessToken,
content))
{
var input = message.Result;
var serializator = new DataContractJsonSerializer(typeof(RequestResponse));
return (RequestResponse)serializator.ReadObject(input.Content.ReadAsStreamAsync().Result);
}
}
}
}
public static FileResponse GetDocument(RequestResponse response)
{
using (var client = new HttpClient())
{
using (var message = client.GetAsync(string.Format("https://developers.dokobit.com/mobile/sign/status/{0}.json?access_token=" + Api.accessToken, response.Token)))
{
var input = message.Result;
var serializator = new DataContractJsonSerializer(typeof(FileResponse));
return (FileResponse)serializator.ReadObject(input.Content.ReadAsStreamAsync().Result);
}
}
}
public static void printResponse(Response response)
{
if (response != null)
{
if (response.Status != null)
{
Console.WriteLine("Status: " + response.Status + "\n");
}
if (response.Message != null)
{
Console.WriteLine("Message: " + response.Message + "\n");
}
if (response.Errors != null && response.Errors.Count() > 0)
{
Console.WriteLine("Errors:\n");
foreach (var error in response.Errors)
{
Console.WriteLine("\t" + error + "\n");
}
}
}
else
{
Console.WriteLine("Failed to receive response\n");
}
}
static void Main(string[] args)
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
string fileName = args.Length > 0 ? args[0] : @"../../test.pdf"; // example pdf file to sign
string phone = args.Length > 1 ? args[1] : "+37060000666"; // enter phone with country code
string code = args.Length > 2 ? args[2] : "50001018865"; // enter personal code
byte[] contentData = System.IO.File.ReadAllBytes(fileName);
var response = Sign(contentData, phone, code);
if (response.Status == "ok")
{
Console.WriteLine("Dokobit WS API signing example. You will receive:\nControl code: {0}, for signing token: {1}", response.ControlCode, response.Token);
FileResponse fileResponse = null;
//Thread.Sleep(30000);
for (int i = 0; i < 30; i++)
{
Console.WriteLine("Waiting");
fileResponse = GetDocument(response);
if (fileResponse.Status != "waiting") break;
Thread.Sleep(1000);
}
if (fileResponse == null || fileResponse.Status != "ok")
{
printResponse(fileResponse);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
return;
}
if (fileResponse.File != null)
{
System.IO.File.WriteAllBytes("test-result.pdf", Convert.FromBase64String(fileResponse.File.Content));
Console.WriteLine("Received response. Open ./test-result.pdf");
}
}
else
{
printResponse(response);
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}