Created
March 21, 2022 21:42
-
-
Save GArrigotti/f5a6b60411770bf57e3f6472475236f3 to your computer and use it in GitHub Desktop.
Generate a Token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TokenRecord GenerateToken(string url, string user, string key) | |
{ | |
var expiration = (long)DateTime.UtcNow.AddMinutes(30).Subtract(DateTime.UnixEpoch).TotalSeconds; | |
var endpoint = HttpUtility.UrlEncode(url); | |
using var hash = new HMACSHA256(Encoding.UTF8.GetBytes(key)); | |
var signature = HttpUtility.UrlEncode(Convert.ToBase64String(hash.ComputeHash(Encoding.UTF8.GetBytes($"{endpoint}\n{expiration}")))); | |
hash.Dispose(); | |
return new TokenRecord($"SharedAccessSignature sr={endpoint}&sig={signature}&se={expiration}&skn={user}", DateTime.UnixEpoch.AddSeconds(expiration)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment