Skip to content

Instantly share code, notes, and snippets.

@princeppy
Forked from vman/Authorize.cs
Created July 18, 2018 13:39
Show Gist options
  • Select an option

  • Save princeppy/19bf58da9373823ed787c2a53a01e01f to your computer and use it in GitHub Desktop.

Select an option

Save princeppy/19bf58da9373823ed787c2a53a01e01f to your computer and use it in GitHub Desktop.
public async Task<ActionResult> Authorize()
{
// Get the 'code' parameter from the Azure redirect
string authCode = Request.Params["code"];
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/common");
// The same url we specified in the auth code request
string redirectUri = Url.Action("Authorize", "Home", null, Request.Url.Scheme);
// Use client ID and secret to establish app identity
ClientCredential credential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
try
{
// Get the token
var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
authCode, new Uri(redirectUri), credential, "https://graph.microsoft.com/");
// Save the token in the session
Session["access_token"] = authResult.AccessToken;
return Redirect(Url.Action("Index", "Home", null, Request.Url.Scheme));
}
catch (AdalException ex)
{
return Content(string.Format("ERROR retrieving token: {0}", ex.Message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment