From 6ca981e4dfae5e17dafc4c5b89313b2a9591ef83 Mon Sep 17 00:00:00 2001 From: Troy Dai Date: Wed, 17 Aug 2016 13:02:28 -0700 Subject: [PATCH] Refine OIDC sample 1. Add signout remote scenario 2. Use bootstrap to enhance the view 3. Improve readability --- .../OpenIdConnect.AzureAdSample/Startup.cs | 107 +++++++++++++----- 1 file changed, 76 insertions(+), 31 deletions(-) diff --git a/samples/OpenIdConnect.AzureAdSample/Startup.cs b/samples/OpenIdConnect.AzureAdSample/Startup.cs index 8a2b7f4412..f0c2f7c221 100644 --- a/samples/OpenIdConnect.AzureAdSample/Startup.cs +++ b/samples/OpenIdConnect.AzureAdSample/Startup.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; @@ -103,47 +105,90 @@ namespace OpenIdConnect.AzureAdSample if (context.Request.Path.Equals("/signout")) { await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"Signing out {context.User.Identity.Name}
{Environment.NewLine}"); - await context.Response.WriteAsync("Sign In"); - await context.Response.WriteAsync($""); - return; + await WriteHtmlAsync(context.Response, + response => response.WriteAsync($"

Signed out locally: {context.User.Identity.Name}

Sign In")); } - - if (!context.User.Identities.Any(identity => identity.IsAuthenticated)) + else if (context.Request.Path.Equals("/signout-remote")) { - await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" }); - return; + await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties + { + RedirectUri = "/remote-signedout" + }); } - - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"Hello Authenticated User {context.User.Identity.Name}
{Environment.NewLine}"); - await context.Response.WriteAsync("Claims:
" + Environment.NewLine); - foreach (var claim in context.User.Claims) + else if (context.Request.Path.Equals("/remote-signedout")) { - await context.Response.WriteAsync($"{claim.Type}: {claim.Value}
{Environment.NewLine}"); + await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await WriteHtmlAsync(context.Response, + response => response.WriteAsync($"

Signed out remotely: {context.User.Identity.Name}

Sign In")); } - - await context.Response.WriteAsync("Tokens:
" + Environment.NewLine); - try + else { - // Use ADAL to get the right token - var authContext = new AuthenticationContext(authority, AuthPropertiesTokenCache.ForApiCalls(context, CookieAuthenticationDefaults.AuthenticationScheme)); - var credential = new ClientCredential(clientId, clientSecret); - string userObjectID = context.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; - var result = await authContext.AcquireTokenSilentAsync(resource, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); + if (!context.User.Identities.Any(identity => identity.IsAuthenticated)) + { + await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" }); + return; + } - await context.Response.WriteAsync($"access_token: {result.AccessToken}
{Environment.NewLine}"); - } - catch (Exception ex) - { - await context.Response.WriteAsync($"AquireToken error: {ex.Message}
{Environment.NewLine}"); - } + await WriteHtmlAsync(context.Response, async response => + { + await response.WriteAsync($"

Hello Authenticated User {context.User.Identity.Name}

"); + await response.WriteAsync("Sign Out Locally"); + await response.WriteAsync("Sign Out Remotely"); - await context.Response.WriteAsync("Sign Out"); - await context.Response.WriteAsync($""); + await response.WriteAsync("

Claims:

"); + await WriteTableHeader(response, new string[] { "Claim Type", "Value" }, context.User.Claims.Select(c => new string[] { c.Type, c.Value })); + + await response.WriteAsync("

Tokens:

"); + try + { + // Use ADAL to get the right token + var authContext = new AuthenticationContext(authority, AuthPropertiesTokenCache.ForApiCalls(context, CookieAuthenticationDefaults.AuthenticationScheme)); + var credential = new ClientCredential(clientId, clientSecret); + string userObjectID = context.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; + var result = await authContext.AcquireTokenSilentAsync(resource, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); + + await response.WriteAsync($"

access_token

{result.AccessToken}
"); + } + catch (Exception ex) + { + await response.WriteAsync($"AquireToken error: {ex.Message}
{Environment.NewLine}"); + } + }); + } }); } + + private static async Task WriteHtmlAsync(HttpResponse response, Func writeContent) + { + var bootstrap = ""; + + response.ContentType = "text/html"; + await response.WriteAsync($"{bootstrap}
"); + await writeContent(response); + await response.WriteAsync("
"); + } + + private static async Task WriteTableHeader(HttpResponse response, IEnumerable columns, IEnumerable> data) + { + await response.WriteAsync(""); + await response.WriteAsync(""); + foreach (var column in columns) + { + await response.WriteAsync($""); + } + await response.WriteAsync(""); + foreach (var row in data) + { + await response.WriteAsync(""); + foreach (var column in row) + { + await response.WriteAsync($""); + } + await response.WriteAsync(""); + } + await response.WriteAsync("
{column}
{column}
"); + } } }