From 727003da12be0e0d6cbca7cf8241867decdd11f9 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Mon, 27 Jul 2020 12:03:33 -0700 Subject: [PATCH] Identity server working --- samples/voting/IdentityServer/Config.cs | 3 +- .../IdentityServer/IdentityServer.csproj | 1 + samples/voting/IdentityServer/Program.cs | 51 +++++++++++-------- samples/voting/IdentityServer/Startup.cs | 11 ++-- samples/voting/results/Pages/Index.razor | 27 ---------- .../results/Properties/launchSettings.json | 2 +- samples/voting/results/Startup.cs | 10 +++- samples/voting/results/results.csproj | 15 ++++++ 8 files changed, 63 insertions(+), 57 deletions(-) diff --git a/samples/voting/IdentityServer/Config.cs b/samples/voting/IdentityServer/Config.cs index 036d749b..f2104185 100644 --- a/samples/voting/IdentityServer/Config.cs +++ b/samples/voting/IdentityServer/Config.cs @@ -19,8 +19,7 @@ namespace IdentityServer public static IEnumerable ApiScopes => new ApiScope[] { - new ApiScope("scope1"), - new ApiScope("scope2"), + new ApiScope("scope"), }; } diff --git a/samples/voting/IdentityServer/IdentityServer.csproj b/samples/voting/IdentityServer/IdentityServer.csproj index e58dda72..103126c0 100644 --- a/samples/voting/IdentityServer/IdentityServer.csproj +++ b/samples/voting/IdentityServer/IdentityServer.csproj @@ -6,6 +6,7 @@ + diff --git a/samples/voting/IdentityServer/Program.cs b/samples/voting/IdentityServer/Program.cs index cf1e4660..4d2805c6 100644 --- a/samples/voting/IdentityServer/Program.cs +++ b/samples/voting/IdentityServer/Program.cs @@ -15,27 +15,38 @@ namespace IdentityServer { public static int Main(string[] args) { - // Log.Logger = new LoggerConfiguration() - // .MinimumLevel.Debug() - // .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) - // .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) - // .MinimumLevel.Override("System", LogEventLevel.Warning) - // .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) - // .Enrich.FromLogContext() - // // uncomment to write to Azure diagnostics stream - // //.WriteTo.File( - // // @"D:\home\LogFiles\Application\identityserver.txt", - // // fileSizeLimitBytes: 1_000_000, - // // rollOnFileSizeLimit: true, - // // shared: true, - // // flushToDiskInterval: TimeSpan.FromSeconds(1)) - // .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code) - // .CreateLogger(); + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) + .MinimumLevel.Override("System", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) + .Enrich.FromLogContext() + // uncomment to write to Azure diagnostics stream + //.WriteTo.File( + // @"D:\home\LogFiles\Application\identityserver.txt", + // fileSizeLimitBytes: 1_000_000, + // rollOnFileSizeLimit: true, + // shared: true, + // flushToDiskInterval: TimeSpan.FromSeconds(1)) + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code) + .CreateLogger(); - // Log.Information("Starting host..."); - CreateHostBuilder(args).Build().Run(); - return 0; - + try + { + Log.Information("Starting host..."); + CreateHostBuilder(args).Build().Run(); + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly."); + return 1; + } + finally + { + Log.CloseAndFlush(); + } } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/samples/voting/IdentityServer/Startup.cs b/samples/voting/IdentityServer/Startup.cs index ab08c8c1..65b836a0 100644 --- a/samples/voting/IdentityServer/Startup.cs +++ b/samples/voting/IdentityServer/Startup.cs @@ -15,6 +15,7 @@ using System; using IdentityServer4.Services; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; namespace IdentityServer { @@ -51,7 +52,7 @@ namespace IdentityServer // in-memory, code config builder.AddInMemoryIdentityResources(Config.IdentityResources); - builder.AddInMemoryApiScopes(Config.ApiScopes); + // builder.AddInMemoryApiScopes(Config.ApiScopes); Console.WriteLine(Configuration.GetServiceUri("results:https")); builder.AddInMemoryClients(new Client[] { @@ -64,13 +65,13 @@ namespace IdentityServer AllowedGrantTypes = GrantTypes.Code, - RedirectUris = { $"{Configuration.GetServiceUri("results:https")}/signin-oidc/" }, - FrontChannelLogoutUri = $"{Configuration.GetServiceUri("results:https")}/signout-oidc/", - PostLogoutRedirectUris = { $"{Configuration.GetServiceUri("results:https")}/signout-callback-oidc/" }, + RedirectUris = { $"{Configuration.GetServiceUri("results:https")}signin-oidc" }, + FrontChannelLogoutUri = $"{Configuration.GetServiceUri("results:https")}signout-oidc", + PostLogoutRedirectUris = { $"{Configuration.GetServiceUri("results:https")}signout-callback-oidc" }, AllowOfflineAccess = true, // AllowedCorsOrigins = { $"{Configuration.GetServiceUri("results:https")}" }, - AllowedScopes = new List{IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile} + AllowedScopes = new List{IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile, "scope"} }, }); diff --git a/samples/voting/results/Pages/Index.razor b/samples/voting/results/Pages/Index.razor index a7d15828..e35e693a 100644 --- a/samples/voting/results/Pages/Index.razor +++ b/samples/voting/results/Pages/Index.razor @@ -12,38 +12,11 @@ @using Microsoft.AspNetCore.Authentication @inject IConfiguration Configuration -@* @inject HttpContext Context -@inject AuthenticationStateProvider AuthenticationStateProvider *@ - -@*
- Dog Votes: @DogVotes.Count - Cat Votes: @CatVotes.Count -
*@
-@*

Claims

- -
- @foreach (var claim in User.Claims) - { -
@claim.Type
-
@claim.Value
- } -
- -

Properties

- -
- @foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items) - { -
@prop.Key
-
@prop.Value
- } -
*@ - @code { private VotingResult DogVotes = new VotingResult { Vote = "a", Count = 0 }; private VotingResult CatVotes = new VotingResult { Vote = "b", Count = 0 }; diff --git a/samples/voting/results/Properties/launchSettings.json b/samples/voting/results/Properties/launchSettings.json index cbdddb87..d80f1500 100644 --- a/samples/voting/results/Properties/launchSettings.json +++ b/samples/voting/results/Properties/launchSettings.json @@ -18,7 +18,7 @@ "results": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "http://localhost:5005", + "applicationUrl": "https://localhost:5005", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/samples/voting/results/Startup.cs b/samples/voting/results/Startup.cs index be74ca33..50d3db10 100644 --- a/samples/voting/results/Startup.cs +++ b/samples/voting/results/Startup.cs @@ -10,6 +10,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.IdentityModel.Tokens.Jwt; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Components.Authorization; +using Results.Areas.Identity; +using Microsoft.EntityFrameworkCore; namespace Results { @@ -26,13 +30,15 @@ namespace Results // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { + services.AddAuthorization(o => o.FallbackPolicy = o.DefaultPolicy); services.AddRazorPages(); services.AddServerSideBlazor(); + services.AddScoped>(); services.AddCors(); JwtSecurityTokenHandler.DefaultMapInboundClaims = false; - + services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; @@ -45,7 +51,6 @@ namespace Results options.ClientId = "interactive"; options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0"; options.ResponseType = "code"; - options.SaveTokens = true; }); } @@ -71,6 +76,7 @@ namespace Results app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); + app.UseEndpoints(endpoints => { endpoints.MapBlazorHub().RequireAuthorization(); diff --git a/samples/voting/results/results.csproj b/samples/voting/results/results.csproj index 98131c23..13ef9224 100644 --- a/samples/voting/results/results.csproj +++ b/samples/voting/results/results.csproj @@ -8,7 +8,9 @@ + + @@ -17,4 +19,17 @@
+ + + + + + + + + + + + +