-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
65 lines (56 loc) · 2.67 KB
/
Program.cs
File metadata and controls
65 lines (56 loc) · 2.67 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
using System;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
using System.Collections.Generic;
namespace GenerateSASToken
{
class Program
{
public static void Main(string[] args)
{
try
{
var parameters = CheckParameters(args);
var id = parameters["id"];
Console.WriteLine($"Identifier informado: {id}");
var key = parameters["key"];
Console.WriteLine($"Primary/Secondary key informado: {key}");
var expiry = DateTime.UtcNow.AddDays(30); // 30 dias suportado
using (var encoder = new HMACSHA512(Encoding.UTF8.GetBytes(key)))
{
var dataToSign = id + "\n" + expiry.ToString("O", CultureInfo.InvariantCulture);
var hash = encoder.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
var signature = Convert.ToBase64String(hash);
var encodedToken = string.Format("SharedAccessSignature uid={0}&ex={1:o}&sn={2}", id, expiry, signature);
Console.WriteLine(encodedToken);
Console.WriteLine("Atribuindo token gerado a variável SASToken...");
Console.WriteLine($"echo ##vso[task.setvariable variable=SASToken]{encodedToken}");
//Environment.SetEnvironmentVariable("SASToken", encodedToken);
//foreach (var variableKey in Environment.GetEnvironmentVariables().Keys)
//{
// Console.WriteLine(variableKey);
//}
//Console.WriteLine("Gerado o Token:{0}", Environment.GetEnvironmentVariable("SASToken"));
}
}
catch (Exception ex)
{
throw new Exception($"Houve erro na geração do Token. Verifique o identifier e o key informados.", ex);
}
}
private static IDictionary<string, string> CheckParameters(string[] args)
{
var parameters = new Dictionary<string, string>();
Console.WriteLine("Verificando o identifier do APIM...");
var id = args[0];
if (string.IsNullOrWhiteSpace(id)) throw new Exception("Identifier do APIM não foi informado!");
parameters.Add("id", id);
Console.WriteLine("Verificando a PrimaryKey ou SecondaryKey do APIM:");
var key = args[1];
if (string.IsNullOrWhiteSpace(key)) throw new Exception("PrimaryKey ou SecondaryKey do APIM não foi informado!");
parameters.Add("key", key);
return parameters;
}
}
}