-
Notifications
You must be signed in to change notification settings - Fork 65
/
Program.cs
78 lines (67 loc) · 2.96 KB
/
Program.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
using Hyak.Common;
using Microsoft.Azure;
using Microsoft.Azure.Management.Redis;
using Microsoft.Azure.Management.Redis.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ManageCacheUsingMAML
{
class Program
{
public static string subscriptionId = ""; //Get this from the Azure Portal
public static string resourceGroupName = "";//Get this from the Azure Portal
public static string cacheName = ""; // This is just the name (without redis.cache.windows.net)
public static string redisSKUName = "Premium"; //Basic/ Standard/ Premium
public static string redisSKUFamily = "P";
public static int redisSKUCapacity = 1; //Cache Size 0-6. More details http://azure.microsoft.com/en-us/pricing/details/cache/
public static string redisCacheRegion = "West US";
// Use the following link to learn how to setup this app to access Active Directory
// https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx#bk_portal
public static string tenantId = "";
public static string clientId = "";
public static string redirectUri = "";
public static string clientSecrets = "";
static void Main(string[] args)
{
//https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx#bk_portal
string token = GetAuthorizationHeader();
TokenCredentials creds = new TokenCredentials(token);
RedisManagementClient client = new RedisManagementClient(creds);
client.SubscriptionId = subscriptionId;
var redisParams = new RedisCreateOrUpdateParameters()
{
Sku = new Sku(redisSKUName, redisSKUFamily, redisSKUCapacity),
Location = redisCacheRegion
};
client.Redis.CreateOrUpdate(resourceGroupName,cacheName, redisParams);
}
private static string GetAuthorizationHeader()
{
AuthenticationResult result = null;
var context = new AuthenticationContext(String.Format("https://login.windows.net/{0}", tenantId));
var thread = new Thread(() =>
{
var authParam = new PlatformParameters(PromptBehavior.Never, null);
result = context.AcquireTokenAsync(
"https://management.core.windows.net/",
new ClientCredential(clientId, clientSecrets)).Result;
});
thread.SetApartmentState(ApartmentState.STA);
thread.Name = "AquireTokenThread";
thread.Start();
thread.Join();
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
return token;
}
}
}