-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventHubService.cs
More file actions
73 lines (64 loc) · 3.21 KB
/
Copy pathEventHubService.cs
File metadata and controls
73 lines (64 loc) · 3.21 KB
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
using DachauTemp.Windows.Models;
using Newtonsoft.Json;
using System;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
namespace Pumpingcode.Services
{
public class EventHubService
{
private const string sasKeyName = "{SAS Key Name}";
private const string sasKeyValue = "{SAS Key Value}";
private const string eventHubName = "{Azure Event Hub name}";
private const string eventHubBaseAddress = "{Azure Event Hub base address}";
private HttpClient httpClient = new HttpClient();
/// <summary>
/// Authenticates at the Event Hub and refreshes the authentication token
/// </summary>
public void Authenticate()
{
TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + 86400); // 86400s = 24h
string stringToSign = WebUtility.UrlEncode(eventHubBaseAddress) + "\n" + expiry;
// Create hash
MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
var hash = provider.CreateHash(CryptographicBuffer.ConvertStringToBinary(sasKeyValue, BinaryStringEncoding.Utf8));
hash.Append(CryptographicBuffer.ConvertStringToBinary(stringToSign, BinaryStringEncoding.Utf8));
// Generate token
var signature = CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());
string token = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
WebUtility.UrlEncode(eventHubBaseAddress), WebUtility.UrlEncode(signature), expiry, sasKeyName);
// Set HTTP Access token
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(token);
}
/// <summary>
/// Sends an object in JSON format to the Event Hub
/// </summary>
public async Task SendEventAsync(object datapoint)
{
// Convert datapoint to JSON
var json = JsonConvert.SerializeObject(datapoint);
var content = new StringContent(json);
// Send event
var response = await httpClient.PostAsync($"{eventHubBaseAddress}/{eventHubName}/messages", content);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
// Maybe token expired available. Renew and try again
RefreshAccessToken();
// Try again
content = new StringContent(json);
response = await httpClient.PostAsync($"{eventHubBaseAddress}/{eventHubName}/messages", content);
}
// Check if everything went fine
if (!response.IsSuccessStatusCode)
throw new UnauthorizedAccessException("Could not connect to Azure Event Hub. Access denied.");
}
}
}