Created
February 14, 2018 09:47
-
-
Save ahelland/065ac981f9a9ddc9f704239825e576ca to your computer and use it in GitHub Desktop.
Implementing the OAuth Deviceprofile Flow with plain HTTP calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string ClientId = "guid-from-portal"; | |
string resource = "00000002-0000-0000-c000-000000000000"; | |
public class DCR | |
{ | |
public string device_code { get; set; } | |
public string message { get; set; } | |
public string user_code { get; set; } | |
public string interval { get; set; } | |
public string expires_in { get; set; } | |
public string verification_url { get; set; } | |
} | |
public class GenericToken | |
{ | |
public string token_type { get; set; } | |
public string scope { get; set; } | |
public string resource { get; set; } | |
public string access_token { get; set; } | |
public string refresh_token { get; set; } | |
public string id_token { get; set; } | |
public string expires_in { get; set; } | |
} | |
[HttpGet] | |
public IActionResult LoginHTTP() | |
{ | |
HttpClient client = new HttpClient(); | |
client.DefaultRequestHeaders.Add("Accept", "application/json"); | |
string requestUrl = $"https://login.microsoftonline.com/common/oauth2/devicecode?resource={resource}&client_id={ClientId}"; | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); | |
try | |
{ | |
HttpResponseMessage response = client.SendAsync(request).Result; | |
string responseString = response.Content.ReadAsStringAsync().Result; | |
DCR dcr = JsonConvert.DeserializeObject<DCR>(responseString); | |
return View(dcr); | |
} | |
catch (Exception x) | |
{ | |
var msg = x.Message; | |
} | |
return View(); | |
} | |
[HttpPost] | |
public IActionResult LoginHTTP(DCR result) | |
{ | |
HttpClient client = new HttpClient(); | |
client.DefaultRequestHeaders.Add("Accept", "application/json"); | |
string requestUrl = $"https://login.microsoftonline.com/common/oauth2/token"; | |
string request_content = $"resource={resource}&client_id={ClientId}&grant_type=device_code&code={result.device_code}"; | |
var pending = true; | |
do | |
{ | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); | |
try | |
{ | |
request.Content = new StringContent(request_content, Encoding.UTF8, "application/x-www-form-urlencoded"); | |
} | |
catch (Exception x) | |
{ | |
var msg = x.Message; | |
} | |
HttpResponseMessage response = client.SendAsync(request).Result; | |
string responseString = response.Content.ReadAsStringAsync().Result; | |
if (response.StatusCode == System.Net.HttpStatusCode.OK) | |
{ | |
GenericToken token = JsonConvert.DeserializeObject<GenericToken>(responseString); | |
pending = false; | |
ViewBag.jwt = token.id_token; | |
return View("MyToken", token); | |
} | |
Thread.Sleep(5000); | |
} while (pending); | |
return View(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment