static string GetDeviceToken(string iothubHostName, string deviceId, string moduleId, string key) { DateTime startTime = DateTime.UtcNow; string audience = WebUtility.UrlEncode($"{iothubHostName}/devices/{deviceId}/modules/{moduleId}"); string expiresOn = SasTokenHelper.BuildExpiresOn(startTime, TimeSpan.FromHours(1)); string data = string.Join("\n", new List <string> { audience, expiresOn }); string signature = Sign(data, key); return(SasTokenHelper.BuildSasToken(audience, signature, expiresOn)); }
public void BuildSasTokenTest() { // Arrange string audience = "testIotHub.azure-devices.net%2Fdevices%2Fdevice1%2Fmodules%2Fmodule1"; string signature = Guid.NewGuid().ToString(); string expiry = "1546304400"; string expectedToken = $"SharedAccessSignature sr=testIotHub.azure-devices.net%2Fdevices%2Fdevice1%2Fmodules%2Fmodule1&sig={signature}&se=1546304400"; // Act string sasToken = SasTokenHelper.BuildSasToken(audience, signature, expiry); // Assert Assert.Equal(expectedToken, sasToken); }