-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathDeviceCode.cshtml.cs
45 lines (41 loc) · 1.99 KB
/
DeviceCode.cshtml.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
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SphereOBOTest.Pages
{
public class DeviceCodeModel : PageModel
{
public async Task<IActionResult> OnGetDeviceLogin()
{
using (HttpClient client = new HttpClient())
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", "0B1C8F7E-28D2-4378-97E2-7D7D63F7C87F"),
new KeyValuePair<string, string>("scope", "https://sphere.azure.net/api/user_impersonation")
});
var responseMessage = await client.PostAsync($"https://login.microsoftonline.com/7d71c83c-ccdf-45b7-b3c9-9c41b94406d9/oauth2/v2.0/devicecode", formContent);
var response = await responseMessage.Content.ReadAsStringAsync();
return new ContentResult { Content = response };
}
}
public async Task<IActionResult> OnGetToken(string code)
{
using (HttpClient client = new HttpClient())
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", "0B1C8F7E-28D2-4378-97E2-7D7D63F7C87F"),
new KeyValuePair<string, string>("scope", "https://sphere.azure.net/api/user_impersonation"),
new KeyValuePair<string, string>("grant_type", "device_code"),
new KeyValuePair<string, string>("device_code", code)
});
var responseMessage = await client.PostAsync($"https://login.microsoftonline.com/7d71c83c-ccdf-45b7-b3c9-9c41b94406d9/oauth2/v2.0/token", formContent);
var response = await responseMessage.Content.ReadAsStringAsync();
return new ContentResult { Content = response };
}
}
}
}