diff --git a/samples/OpenIdConnectSample/Program.cs b/samples/OpenIdConnectSample/Program.cs index b370c85a9e..49cbf139d6 100644 --- a/samples/OpenIdConnectSample/Program.cs +++ b/samples/OpenIdConnectSample/Program.cs @@ -13,7 +13,7 @@ namespace OpenIdConnectSample var host = new WebHostBuilder() .UseKestrel(options => { - //Configure SSL + // Configure SSL var serverCertificate = LoadCertificate(); options.UseHttps(serverCertificate); }) diff --git a/samples/OpenIdConnectSample/Startup.cs b/samples/OpenIdConnectSample/Startup.cs index 32d4739d19..37b753102b 100644 --- a/samples/OpenIdConnectSample/Startup.cs +++ b/samples/OpenIdConnectSample/Startup.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Text.Encodings.Web; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; @@ -80,20 +83,22 @@ namespace OpenIdConnectSample { if (context.Request.Path.Equals("/signedout")) { - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"You have been signed out.
{Environment.NewLine}"); - await context.Response.WriteAsync("Sign In"); - await context.Response.WriteAsync($""); + await WriteHtmlAsync(context.Response, async res => + { + await res.WriteAsync($"

You have been signed out.

"); + await res.WriteAsync("Sign In"); + }); return; } if (context.Request.Path.Equals("/signout")) { await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"Signed out {context.User.Identity.Name}
{Environment.NewLine}"); - await context.Response.WriteAsync("Sign In"); - await context.Response.WriteAsync($""); + await WriteHtmlAsync(context.Response, async res => + { + await context.Response.WriteAsync($"

Signed out {HtmlEncode(context.User.Identity.Name)}

"); + await context.Response.WriteAsync("Sign In"); + }); return; } @@ -111,10 +116,11 @@ namespace OpenIdConnectSample if (context.Request.Path.Equals("/Account/AccessDenied")) { await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"Access Denied for user {context.User.Identity.Name} to resource '{context.Request.Query["ReturnUrl"]}'
{Environment.NewLine}"); - await context.Response.WriteAsync("Sign Out"); - await context.Response.WriteAsync($""); + await WriteHtmlAsync(context.Response, async res => + { + await context.Response.WriteAsync($"

Access Denied for user {HtmlEncode(context.User.Identity.Name)} to resource '{HtmlEncode(context.Request.Query["ReturnUrl"])}'

"); + await context.Response.WriteAsync("Sign Out"); + }); return; } @@ -147,18 +153,53 @@ namespace OpenIdConnectSample return; } - context.Response.ContentType = "text/html"; - await context.Response.WriteAsync($"Hello Authenticated User {user.Identity.Name}
{Environment.NewLine}"); - foreach (var claim in user.Claims) + + await WriteHtmlAsync(context.Response, async response => { - await context.Response.WriteAsync($"{claim.Type}: {claim.Value}
{Environment.NewLine}"); - } - await context.Response.WriteAsync("Restricted
"); - await context.Response.WriteAsync("Sign Out
"); - await context.Response.WriteAsync("Sign Out Remote
"); - await context.Response.WriteAsync($""); + await response.WriteAsync($"

Hello Authenticated User {HtmlEncode(user.Identity.Name)}

"); + await response.WriteAsync("Restricted"); + await response.WriteAsync("Sign Out"); + await response.WriteAsync("Sign Out Remote"); + + await response.WriteAsync("

Claims:

"); + await WriteTableHeader(response, new string[] { "Claim Type", "Value" }, context.User.Claims.Select(c => new string[] { c.Type, c.Value })); + }); }); } + + 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("
{HtmlEncode(column)}
{HtmlEncode(column)}
"); + } + + private static string HtmlEncode(string content) => + string.IsNullOrEmpty(content) ? string.Empty : HtmlEncoder.Default.Encode(content); } }