-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathProgram.cs
88 lines (74 loc) · 2.86 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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using IdentityModel.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SampleClient
{
public class PhoneNumberVerifyResponse
{
[JsonProperty("resend_token")] public string ResendToken { get; set; }
[JsonProperty("verify_token")] public string VerifyToken { get; set; }
}
class Program
{
public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
private static async Task MainAsync()
{
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("http://localhost:62537");
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
var contentObject = JsonConvert.SerializeObject(new Dictionary<string, string>
{
{"phonenumber", "+198989822"}
});
var stringContent = new StringContent(contentObject, Encoding.UTF8, "application/json");
var phoneNumberResult =
await client.PostAsync("http://localhost:62537/api/verify_phone_number", stringContent);
var verifyTokenResponseString = await phoneNumberResult.Content.ReadAsStringAsync();
var verifyTokenResponse =
JsonConvert.DeserializeObject<PhoneNumberVerifyResponse>(verifyTokenResponseString);
// request token
var tokenRequest = new TokenRequest
{
GrantType = "phone_number_token",
ClientId = "phone_number_authentication",
ClientSecret = "secret",
Address = disco.TokenEndpoint,
Parameters = new Dictionary<string, string>
{
{"phone_number", "+198989822"},
{"verification_token", verifyTokenResponse.VerifyToken}
}
};
var tokenResponse = await client.RequestTokenAsync(tokenRequest);
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
// call api
var apiClient = new HttpClient();
apiClient.SetBearerToken(tokenResponse.AccessToken);
var response = await apiClient.GetAsync("http://localhost:62732/api/identity");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
}
}
}