Created
February 14, 2018 09:50
-
-
Save ahelland/2a19ad5c165987ba05c928ef98baeac3 to your computer and use it in GitHub Desktop.
Implementing the OAuth Deviceprofile Flow using ADAL
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
[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