Beispiel #1
0
        public void BuildExpiresOnTest()
        {
            // Arrange
            var      startTime = new DateTime(2019, 01, 01);
            TimeSpan ttl       = TimeSpan.FromHours(1);

            // Act
            string expiresOn = SasTokenHelper.BuildExpiresOn(startTime, ttl);

            // Assert
            Assert.Equal("1546304400", expiresOn);
        }
        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 async Task SignTest()
        {
            // Arrange
            string audience  = SasTokenHelper.BuildAudience("foo.azure-devices.net", "ed1", "$edgeHub");
            string expiresOn = SasTokenHelper.BuildExpiresOn(new DateTime(2018, 01, 01), TimeSpan.FromHours(1));
            string data      = string.Join("\n", new List <string> {
                audience, expiresOn
            });
            string key               = Convert.ToBase64String(Encoding.UTF8.GetBytes("DummyKey123"));
            string expectedToken     = Convert.ToBase64String(new HMACSHA256(Convert.FromBase64String(key)).ComputeHash(Encoding.UTF8.GetBytes(data)));
            var    signatureProvider = new SharedAccessKeySignatureProvider(key);

            // Act
            string token = await signatureProvider.SignAsync(data);

            // Assert
            Assert.Equal(expectedToken, token);
        }