|
1 | 1 | using Microsoft.Rest;
|
2 |
| -using System; |
3 | 2 | using Xunit;
|
4 | 3 | using System.Net.Http.Headers;
|
5 | 4 | using System.Threading;
|
6 | 5 | 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; |
7 | 11 |
|
8 | 12 | namespace Azure.Experiments.Tests
|
9 | 13 | {
|
10 | 14 | public class ComputeTest
|
11 | 15 | {
|
12 | 16 | 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( |
15 | 32 | CancellationToken cancellationToken)
|
16 | 33 | {
|
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; |
18 | 50 | }
|
| 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; |
19 | 71 | }
|
20 | 72 |
|
21 | 73 | [Fact]
|
22 |
| - public void Test1() |
| 74 | + public async void Test1() |
23 | 75 | {
|
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(); |
26 | 82 | }
|
27 | 83 | }
|
28 | 84 | }
|
0 commit comments