@@ -18,9 +18,10 @@ namespace Microsoft.Azure.Commands.Management.IotHub
18
18
using System . Collections . Generic ;
19
19
using System . Globalization ;
20
20
using System . Management . Automation ;
21
+ using System . Net ;
21
22
using System . Security . Cryptography ;
22
23
using System . Text ;
23
- using System . Web ;
24
+ using System . Text . RegularExpressions ;
24
25
using Microsoft . Azure . Commands . Management . IotHub . Common ;
25
26
using Microsoft . Azure . Commands . Management . IotHub . Models ;
26
27
using Microsoft . Azure . Devices ;
@@ -179,15 +180,16 @@ private string createToken(string resourceUri, string keyName, string key, int d
179
180
{
180
181
TimeSpan sinceEpoch = DateTime . UtcNow - new DateTime ( 1970 , 1 , 1 ) ;
181
182
var expiry = Convert . ToString ( ( int ) sinceEpoch . TotalSeconds + duration ) ;
182
- string stringToSign = HttpUtility . UrlEncode ( resourceUri ) + "\n " + expiry ;
183
- HMACSHA256 hmac = new HMACSHA256 ( Encoding . UTF8 . GetBytes ( key ) ) ;
183
+ string stringToSign = WebUtility . UrlEncode ( resourceUri ) + "\n " + expiry ;
184
+ stringToSign = Regex . Replace ( stringToSign , "(%[0-9A-F]{2})" , c => c . Value . ToLowerInvariant ( ) ) ;
185
+ HMACSHA256 hmac = new HMACSHA256 ( Convert . FromBase64String ( key ) ) ;
184
186
var signature = Convert . ToBase64String ( hmac . ComputeHash ( Encoding . UTF8 . GetBytes ( stringToSign ) ) ) ;
185
- var sasToken = String . Format ( CultureInfo . InvariantCulture , "SharedAccessSignature sr={0}&sig={1}&se={2}" , HttpUtility . UrlEncode ( resourceUri ) , HttpUtility . UrlEncode ( signature ) , expiry ) ;
187
+ var sasToken = String . Format ( CultureInfo . InvariantCulture , "SharedAccessSignature sr={0}&sig={1}&se={2}" , WebUtility . UrlEncode ( resourceUri ) , WebUtility . UrlEncode ( signature ) , expiry ) ;
186
188
if ( ! string . IsNullOrEmpty ( keyName ) )
187
189
{
188
190
sasToken += String . Format ( CultureInfo . InvariantCulture , "&skn={0}" , keyName ) ;
189
191
}
190
- return sasToken ;
192
+ return Regex . Replace ( sasToken , "(%[0-9A-F]{2})" , c => c . Value . ToLowerInvariant ( ) ) ;
191
193
}
192
194
}
193
195
}
0 commit comments