This repository has been archived by the owner on Dec 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Program.cs
240 lines (202 loc) · 10.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
The MIT License (MIT)
Copyright (c) 2018 Microsoft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// The following using statements were added for this sample.
using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TodoListDaemonWithCert
{
class Program
{
//
// The Client ID is used by the application to uniquely identify itself to Azure AD.
// The Cert Name is the subject name of the certificate used to authenticate this application to Azure AD.
// The Tenant is the name of the Azure AD tenant in which this application is registered.
// The AAD Instance is the instance of Azure, for example public Azure or Azure China.
// The Authority is the sign-in URL of the tenant.
//
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string certName = ConfigurationManager.AppSettings["ida:CertName"];
static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
//
// To authenticate to the To Do list service, the client needs to know the service's App ID URI.
// To contact the To Do list service we need it's URL as well.
//
private static string todoListResourceId = ConfigurationManager.AppSettings["todo:TodoListResourceId"];
private static string todoListBaseAddress = ConfigurationManager.AppSettings["todo:TodoListBaseAddress"];
private static HttpClient httpClient = new HttpClient();
private static AuthenticationContext authContext = null;
private static ClientAssertionCertificate certCred = null;
private static int errorCode;
static int Main(string[] args)
{
// Return code so that exceptions provoke a non null return code for the daemon
errorCode = 0;
// Create the authentication context to be used to acquire tokens.
authContext = new AuthenticationContext(authority);
// Initialize the Certificate Credential to be used by ADAL.
X509Certificate2 cert = ReadCertificateFromStore(certName);
if (cert == null)
{
Console.WriteLine($"Cannot find active certificate '{certName}' in certificates for current user. Please check configuration");
return -1;
}
// Then create the certificate credential client assertion.
certCred = new ClientAssertionCertificate(clientId, cert);
// Call the ToDoList service 10 times with short delay between calls.
int delay = 1000;
for (int i = 0; i < 10; i++)
{
PostNewTodoItem().Wait();
Thread.Sleep(delay);
DisplayTodoList().Wait();
Thread.Sleep(delay);
}
return errorCode;
}
/// <summary>
/// Reads the certificate
/// </summary>
private static X509Certificate2 ReadCertificateFromStore(string certName)
{
X509Certificate2 cert = null;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = store.Certificates;
// Find unexpired certificates.
X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
// From the collection of unexpired certificates, find the ones with the correct name.
X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindBySubjectDistinguishedName, certName, false);
// Return the first certificate in the collection, has the right name and is current.
cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
store.Close();
return cert;
}
/// <summary>
/// Get an access token from Azure AD using client credentials.
/// If the attempt to get a token fails because the server is unavailable, retry twice after 3 seconds each
/// </summary>
private static async Task<AuthenticationResult> GetAccessToken(string todoListResourceId)
{
//
// Get an access token from Azure AD using client credentials.
// If the attempt to get a token fails because the server is unavailable, retry twice after 3 seconds each.
//
AuthenticationResult result = null;
int retryCount = 0;
bool retry = false;
do
{
retry = false;
errorCode = 0;
try
{
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
result = await authContext.AcquireTokenAsync(todoListResourceId, certCred);
}
catch (AdalException ex)
{
if (ex.ErrorCode == "temporarily_unavailable")
{
retry = true;
retryCount++;
Thread.Sleep(3000);
}
Console.WriteLine(
String.Format("An error occurred while acquiring a token\nTime: {0}\nError: {1}\nRetry: {2}\n",
DateTime.Now.ToString(),
ex.ToString(),
retry.ToString()));
errorCode = -1;
}
} while ((retry == true) && (retryCount < 3));
return result;
}
/// <summary>
/// Post a Todo item to the TodoList service
/// </summary>
static async Task PostNewTodoItem()
{
// Get a token
AuthenticationResult result = await GetAccessToken(todoListResourceId);
if (result == null)
{
Console.WriteLine("Canceling attempt to contact To Do list service.\n");
return;
}
// Add the access token to the authorization header of the request.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
// Posts a new item by calling the API using the http client
string timeNow = DateTime.Now.ToString();
string todoText = "Task at time: " + timeNow;
// Forms encode To Do item and POST to the todo list web api.
Console.WriteLine("Posting to To Do list at {0}", timeNow);
HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", todoText) });
HttpResponseMessage response = await httpClient.PostAsync(todoListBaseAddress + "/api/todolist", content);
if (response.IsSuccessStatusCode == true)
{
Console.WriteLine("Successfully posted new To Do item: {0}\n", todoText);
}
else
{
Console.WriteLine("Failed to post a new To Do item\nError: {0}\n", response.ReasonPhrase);
}
}
/// <summary>
/// Display the list of todo items by querying the todolist service
/// </summary>
/// <returns></returns>
static async Task DisplayTodoList()
{
AuthenticationResult result = await GetAccessToken(todoListResourceId);
// Add the access token to the authorization header of the request.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
// Call the To Do list service.
Console.WriteLine("Retrieving To Do list at {0}", DateTime.Now.ToString());
HttpResponseMessage response = await httpClient.GetAsync(todoListBaseAddress + "/api/todolist");
if (response.IsSuccessStatusCode)
{
// Read the response and output it to the console.
string s = await response.Content.ReadAsStringAsync();
List<TodoItem> toDoArray = JsonConvert.DeserializeObject<List<TodoItem>>(s);
foreach (TodoItem item in toDoArray)
{
Console.WriteLine(item.Title);
}
Console.WriteLine("Total item count: {0}\n", toDoArray.Count);
}
else
{
Console.WriteLine("Failed to retrieve To Do list\nError: {0}\n", response.ReasonPhrase);
}
}
}
}