Skip to content

Commit cf6fd68

Browse files
First test
1 parent 0a78c02 commit cf6fd68

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed
Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,84 @@
11
using Microsoft.Rest;
2-
using System;
32
using Xunit;
43
using System.Net.Http.Headers;
54
using System.Threading;
65
using System.Threading.Tasks;
6+
using System.Net.Http;
7+
using System;
8+
using System.IO;
9+
using Newtonsoft.Json;
10+
using Microsoft.Azure.Management.Compute;
711

812
namespace Azure.Experiments.Tests
913
{
1014
public class ComputeTest
1115
{
1216
sealed class TokenProvider : ITokenProvider
13-
{
14-
public Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(
17+
{
18+
public TokenProvider(Configuration c)
19+
{
20+
var parameters = new[]
21+
{
22+
KeyValuePair.Create("grant_type", "client_credentials"),
23+
KeyValuePair.Create("client_id", c.applicationId),
24+
KeyValuePair.Create("client_secret", c.clientSecret),
25+
KeyValuePair.Create("resource", "https://management.core.windows.net/")
26+
};
27+
Content = new FormUrlEncodedContent(parameters);
28+
Uri = new Uri("https://login.microsoftonline.com/" + c.tenantId + "/oauth2/token");
29+
}
30+
31+
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(
1532
CancellationToken cancellationToken)
1633
{
17-
throw new NotImplementedException();
34+
if (Header == null)
35+
{
36+
using (var client = new HttpClient())
37+
{
38+
var response = await client
39+
.PostAsync(Uri, Content)
40+
.Result
41+
.Content
42+
.ReadAsStringAsync();
43+
var responseObject = JsonConvert.DeserializeObject<AuthenticationResponse>(
44+
response);
45+
Header = new AuthenticationHeaderValue(
46+
responseObject.token_type, responseObject.access_token);
47+
}
48+
}
49+
return Header;
1850
}
51+
52+
private Uri Uri { get; }
53+
54+
private FormUrlEncodedContent Content { get; }
55+
56+
private AuthenticationHeaderValue Header { get; set; }
57+
}
58+
59+
private sealed class AuthenticationResponse
60+
{
61+
public string token_type;
62+
public string access_token;
63+
}
64+
65+
private sealed class Configuration
66+
{
67+
public string applicationId;
68+
public string tenantId;
69+
public string clientSecret;
70+
public string subscriptionId;
1971
}
2072

2173
[Fact]
22-
public void Test1()
74+
public async void Test1()
2375
{
24-
var credentials = new TokenCredentials(new TokenProvider());
25-
var client = new Microsoft.Azure.Management.Compute.ComputeManagementClient(credentials);
76+
var text = File.ReadAllText(@"c:\Users\sergey\Desktop\php-test.json");
77+
var c = JsonConvert.DeserializeObject<Configuration>(text);
78+
var credentials = new TokenCredentials(new TokenProvider(c));
79+
var client = new ComputeManagementClient(credentials);
80+
client.SubscriptionId = c.subscriptionId;
81+
var list = await client.VirtualMachines.ListAllAsync();
2682
}
2783
}
2884
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace Azure.Experiments.Tests
4+
{
5+
internal static class KeyValuePair
6+
{
7+
public static KeyValuePair<K, V> Create<K, V>(K k, V v)
8+
=> new KeyValuePair<K, V>(k, v);
9+
}
10+
}

0 commit comments

Comments
 (0)