Skip to content

Instantly share code, notes, and snippets.

@trailmax
Created September 9, 2015 20:41
Show Gist options
  • Save trailmax/c782ae9a69ac76ae6eb1 to your computer and use it in GitHub Desktop.
Save trailmax/c782ae9a69ac76ae6eb1 to your computer and use it in GitHub Desktop.
public partial class Startup
{
private static string clientId = CloudConfigurationManager.GetSetting("ida:ClientId");
private static string appKey = CloudConfigurationManager.GetSetting("ida:ClientSecret");
private static string aadInstance = CloudConfigurationManager.GetSetting("ida:AADInstance");
private static string tenantId = CloudConfigurationManager.GetSetting("ida:TenantId");
private static string postLogoutRedirectUri = CloudConfigurationManager.GetSetting("ida:PostLogoutRedirectUri");
public static readonly string Authority = aadInstance + tenantId;
// This is the resource ID of the AAD Graph API. We'll need this to request a token to call the Graph API.
string graphResourceId = "https://graph.windows.net";
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(Authority);
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
return Task.FromResult(0);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment