Browse Source

Identity server working

jkotalik/newVotingSample
Justin Kotalik 6 years ago
parent
commit
727003da12
  1. 3
      samples/voting/IdentityServer/Config.cs
  2. 1
      samples/voting/IdentityServer/IdentityServer.csproj
  3. 51
      samples/voting/IdentityServer/Program.cs
  4. 11
      samples/voting/IdentityServer/Startup.cs
  5. 27
      samples/voting/results/Pages/Index.razor
  6. 2
      samples/voting/results/Properties/launchSettings.json
  7. 10
      samples/voting/results/Startup.cs
  8. 15
      samples/voting/results/results.csproj

3
samples/voting/IdentityServer/Config.cs

@ -19,8 +19,7 @@ namespace IdentityServer
public static IEnumerable<ApiScope> ApiScopes => public static IEnumerable<ApiScope> ApiScopes =>
new ApiScope[] new ApiScope[]
{ {
new ApiScope("scope1"), new ApiScope("scope"),
new ApiScope("scope2"),
}; };
} }

1
samples/voting/IdentityServer/IdentityServer.csproj

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="IdentityServer4" Version="4.0.0" /> <PackageReference Include="IdentityServer4" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.5" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.5" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />

51
samples/voting/IdentityServer/Program.cs

@ -15,27 +15,38 @@ namespace IdentityServer
{ {
public static int Main(string[] args) public static int Main(string[] args)
{ {
// Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
// .MinimumLevel.Debug() .MinimumLevel.Debug()
// .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
// .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
// .MinimumLevel.Override("System", LogEventLevel.Warning) .MinimumLevel.Override("System", LogEventLevel.Warning)
// .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
// .Enrich.FromLogContext() .Enrich.FromLogContext()
// // uncomment to write to Azure diagnostics stream // uncomment to write to Azure diagnostics stream
// //.WriteTo.File( //.WriteTo.File(
// // @"D:\home\LogFiles\Application\identityserver.txt", // @"D:\home\LogFiles\Application\identityserver.txt",
// // fileSizeLimitBytes: 1_000_000, // fileSizeLimitBytes: 1_000_000,
// // rollOnFileSizeLimit: true, // rollOnFileSizeLimit: true,
// // shared: true, // shared: true,
// // flushToDiskInterval: TimeSpan.FromSeconds(1)) // flushToDiskInterval: TimeSpan.FromSeconds(1))
// .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code) .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
// .CreateLogger(); .CreateLogger();
// Log.Information("Starting host..."); try
CreateHostBuilder(args).Build().Run(); {
return 0; 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) => public static IHostBuilder CreateHostBuilder(string[] args) =>

11
samples/voting/IdentityServer/Startup.cs

@ -15,6 +15,7 @@ using System;
using IdentityServer4.Services; using IdentityServer4.Services;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace IdentityServer namespace IdentityServer
{ {
@ -51,7 +52,7 @@ namespace IdentityServer
// in-memory, code config // in-memory, code config
builder.AddInMemoryIdentityResources(Config.IdentityResources); builder.AddInMemoryIdentityResources(Config.IdentityResources);
builder.AddInMemoryApiScopes(Config.ApiScopes); // builder.AddInMemoryApiScopes(Config.ApiScopes);
Console.WriteLine(Configuration.GetServiceUri("results:https")); Console.WriteLine(Configuration.GetServiceUri("results:https"));
builder.AddInMemoryClients(new Client[] builder.AddInMemoryClients(new Client[]
{ {
@ -64,13 +65,13 @@ namespace IdentityServer
AllowedGrantTypes = GrantTypes.Code, AllowedGrantTypes = GrantTypes.Code,
RedirectUris = { $"{Configuration.GetServiceUri("results:https")}/signin-oidc/" }, RedirectUris = { $"{Configuration.GetServiceUri("results:https")}signin-oidc" },
FrontChannelLogoutUri = $"{Configuration.GetServiceUri("results:https")}/signout-oidc/", FrontChannelLogoutUri = $"{Configuration.GetServiceUri("results:https")}signout-oidc",
PostLogoutRedirectUris = { $"{Configuration.GetServiceUri("results:https")}/signout-callback-oidc/" }, PostLogoutRedirectUris = { $"{Configuration.GetServiceUri("results:https")}signout-callback-oidc" },
AllowOfflineAccess = true, AllowOfflineAccess = true,
// AllowedCorsOrigins = { $"{Configuration.GetServiceUri("results:https")}" }, // AllowedCorsOrigins = { $"{Configuration.GetServiceUri("results:https")}" },
AllowedScopes = new List<string>{IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile} AllowedScopes = new List<string>{IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile, "scope"}
}, },
}); });

27
samples/voting/results/Pages/Index.razor

@ -12,38 +12,11 @@
@using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Authentication
@inject IConfiguration Configuration @inject IConfiguration Configuration
@* @inject HttpContext Context
@inject AuthenticationStateProvider AuthenticationStateProvider *@
@* <div id="result">
<span>Dog Votes: @DogVotes.Count</span>
<span>Cat Votes: @CatVotes.Count</span>
</div> *@
<div id="result"> <div id="result">
<ChartJsPieChart @ref="_pieChartJs" Config="@_config" Width="600" Height="300" /> <ChartJsPieChart @ref="_pieChartJs" Config="@_config" Width="600" Height="300" />
</div> </div>
@* <h2>Claims</h2>
<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
</dl>
<h2>Properties</h2>
<dl>
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>@prop.Key</dt>
<dd>@prop.Value</dd>
}
</dl> *@
@code { @code {
private VotingResult DogVotes = new VotingResult { Vote = "a", Count = 0 }; private VotingResult DogVotes = new VotingResult { Vote = "a", Count = 0 };
private VotingResult CatVotes = new VotingResult { Vote = "b", Count = 0 }; private VotingResult CatVotes = new VotingResult { Vote = "b", Count = 0 };

2
samples/voting/results/Properties/launchSettings.json

@ -18,7 +18,7 @@
"results": { "results": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "http://localhost:5005", "applicationUrl": "https://localhost:5005",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

10
samples/voting/results/Startup.cs

@ -10,6 +10,10 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Components.Authorization;
using Results.Areas.Identity;
using Microsoft.EntityFrameworkCore;
namespace Results 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 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddAuthorization(o => o.FallbackPolicy = o.DefaultPolicy);
services.AddRazorPages(); services.AddRazorPages();
services.AddServerSideBlazor(); services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddCors(); services.AddCors();
JwtSecurityTokenHandler.DefaultMapInboundClaims = false; JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
options.DefaultScheme = "Cookies"; options.DefaultScheme = "Cookies";
@ -45,7 +51,6 @@ namespace Results
options.ClientId = "interactive"; options.ClientId = "interactive";
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0"; options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
options.ResponseType = "code"; options.ResponseType = "code";
options.SaveTokens = true; options.SaveTokens = true;
}); });
} }
@ -71,6 +76,7 @@ namespace Results
app.UseRouting(); app.UseRouting();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapBlazorHub().RequireAuthorization(); endpoints.MapBlazorHub().RequireAuthorization();

15
samples/voting/results/results.csproj

@ -8,7 +8,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="ChartJs.Blazor" Version="1.0.3" /> <PackageReference Include="ChartJs.Blazor" Version="1.0.3" />
<PackageReference Include="dapper" Version="2.0.30" /> <PackageReference Include="dapper" Version="2.0.30" />
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
<PackageReference Include="microsoft.aspnetcore.signalr.client" Version="3.1.3" /> <PackageReference Include="microsoft.aspnetcore.signalr.client" Version="3.1.3" />
<PackageReference Include="npgsql" Version="4.1.3.1" /> <PackageReference Include="npgsql" Version="4.1.3.1" />
</ItemGroup> </ItemGroup>
@ -17,4 +19,17 @@
<ProjectReference Include="$(TyeLibrariesPath)\Microsoft.Tye.Extensions.Configuration\Microsoft.Tye.Extensions.Configuration.csproj" /> <ProjectReference Include="$(TyeLibrariesPath)\Microsoft.Tye.Extensions.Configuration\Microsoft.Tye.Extensions.Configuration.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save