-
Notifications
You must be signed in to change notification settings - Fork 163
/
Program.cs
103 lines (84 loc) · 3.58 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel;
using IdentityModel.Client;
using Newtonsoft.Json.Linq;
namespace Client
{
class Program
{
static IDiscoveryCache _cache = new DiscoveryCache("https://demo.identityserver.io");
public static async Task Main()
{
Console.Title = "Console Device Flow";
var authorizeResponse = await RequestAuthorizationAsync();
var tokenResponse = await RequestTokenAsync(authorizeResponse);
Console.WriteLine(tokenResponse.AccessToken);
//tokenResponse.Show();
Console.ReadLine();
await CallServiceAsync(tokenResponse.AccessToken);
}
static async Task<DeviceAuthorizationResponse> RequestAuthorizationAsync()
{
var disco = await _cache.GetAsync();
if (disco.IsError) throw new Exception(disco.Error);
var client = new HttpClient();
var response = await client.RequestDeviceAuthorizationAsync(new DeviceAuthorizationRequest
{
Address = disco.DeviceAuthorizationEndpoint,
ClientId = "device"
});
if (response.IsError) throw new Exception(response.Error);
Console.WriteLine($"user code : {response.UserCode}");
Console.WriteLine($"device code : {response.DeviceCode}");
Console.WriteLine($"URL : {response.VerificationUri}");
Console.WriteLine($"Complete URL: {response.VerificationUriComplete}");
Console.WriteLine($"\nPress enter to launch browser ({response.VerificationUri})");
Console.ReadLine();
Process.Start(new ProcessStartInfo(response.VerificationUri) { UseShellExecute = true });
return response;
}
private static async Task<TokenResponse> RequestTokenAsync(DeviceAuthorizationResponse authorizeResponse)
{
var disco = await _cache.GetAsync();
if (disco.IsError) throw new Exception(disco.Error);
var client = new HttpClient();
while (true)
{
var response = await client.RequestDeviceTokenAsync(new DeviceTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "device",
DeviceCode = authorizeResponse.DeviceCode
});
if (response.IsError)
{
if (response.Error == OidcConstants.TokenErrors.AuthorizationPending || response.Error == OidcConstants.TokenErrors.SlowDown)
{
Console.WriteLine($"{response.Error}...waiting.");
await Task.Delay(authorizeResponse.Interval * 1000);
}
else
{
throw new Exception(response.Error);
}
}
else
{
return response;
}
}
}
static async Task CallServiceAsync(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var response = await client.GetStringAsync("http://localhost:3308/test");
Console.WriteLine(JArray.Parse(response));
}
}
}