Skip to content

Instantly share code, notes, and snippets.

@vman
Created May 8, 2015 20:38
Show Gist options
  • Select an option

  • Save vman/6576b3d57ba71c3299b8 to your computer and use it in GitHub Desktop.

Select an option

Save vman/6576b3d57ba71c3299b8 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