Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created February 14, 2018 09:50
Show Gist options
  • Save ahelland/2a19ad5c165987ba05c928ef98baeac3 to your computer and use it in GitHub Desktop.
Save ahelland/2a19ad5c165987ba05c928ef98baeac3 to your computer and use it in GitHub Desktop.
Implementing the OAuth Deviceprofile Flow using ADAL
[HttpGet]
public async Task<IActionResult> LoginADAL()
{
AuthenticationContext ctx = null;
ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
DeviceCodeResult codeResult = ctx.AcquireDeviceCodeAsync(resource, ClientId).Result;
DCR dcr = new DCR { message = codeResult.Message,
device_code = codeResult.DeviceCode,
expires_in = codeResult.ExpiresOn.ToString(),
interval = codeResult.Interval.ToString(),
user_code = codeResult.UserCode,
verification_url = codeResult.VerificationUrl };
//Will run in the background after view has been rendered
var foo = GetADALToken(codeResult);
return View(dcr);
}
public async Task<string> GetADALToken(DeviceCodeResult codeResult)
{
AuthenticationContext ctx = null;
AuthenticationResult result = null;
ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
try
{
result = await ctx.AcquireTokenByDeviceCodeAsync(codeResult);
}
catch (Exception exc)
{
var error = exc.Message;
}
return result.IdToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment