Skip to content

Commit 794cd67

Browse files
committed
Migration to PostgreSQL. Configuration
1 parent 6a86ed7 commit 794cd67

12 files changed

+217
-247
lines changed

IdentityServer.PostgreSQL.sln

-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StsServerIdentity", "src\St
2424
EndProject
2525
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngularClientCode", "src\AngularClientCode\AngularClientCode.csproj", "{D97F476C-CD83-432E-AC15-077C91F78B79}"
2626
EndProject
27-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngularClient1", "src\AngularClient1\AngularClient1.csproj", "{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB}"
28-
EndProject
2927
Global
3028
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3129
Debug|Any CPU = Debug|Any CPU
@@ -60,10 +58,6 @@ Global
6058
{D97F476C-CD83-432E-AC15-077C91F78B79}.Debug|Any CPU.Build.0 = Debug|Any CPU
6159
{D97F476C-CD83-432E-AC15-077C91F78B79}.Release|Any CPU.ActiveCfg = Release|Any CPU
6260
{D97F476C-CD83-432E-AC15-077C91F78B79}.Release|Any CPU.Build.0 = Release|Any CPU
63-
{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64-
{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
65-
{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
66-
{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB}.Release|Any CPU.Build.0 = Release|Any CPU
6761
EndGlobalSection
6862
GlobalSection(SolutionProperties) = preSolution
6963
HideSolutionNode = FALSE
@@ -76,7 +70,6 @@ Global
7670
{93DB1A95-88E1-4B5C-909B-012D64350251} = {A0F6C07F-188D-4F86-B43F-4A3EAD0D2C30}
7771
{19DF053D-2D66-4502-A5C2-341EF981D0C0} = {A0F6C07F-188D-4F86-B43F-4A3EAD0D2C30}
7872
{D97F476C-CD83-432E-AC15-077C91F78B79} = {A0F6C07F-188D-4F86-B43F-4A3EAD0D2C30}
79-
{3E9F1A73-41EC-474C-A07C-DA0DD3F428FB} = {A0F6C07F-188D-4F86-B43F-4A3EAD0D2C30}
8073
EndGlobalSection
8174
GlobalSection(ExtensibilityGlobals) = postSolution
8275
SolutionGuid = {269BE43F-2664-4A83-931C-2EC5E886B8AB}

src/AngularClient/Properties/launchSettings.json

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
{
2-
//"iisSettings": {
3-
// "windowsAuthentication": false,
4-
// "anonymousAuthentication": true,
5-
// "iisExpress": {
6-
// "applicationUrl": "http://localhost:59819",
7-
// "sslPort": 44383
8-
// }
9-
//},
102
"profiles": {
11-
//"IIS Express": {
12-
// "commandName": "IISExpress",
13-
// "launchBrowser": true,
14-
// "environmentVariables": {
15-
// "ASPNETCORE_ENVIRONMENT": "Development"
16-
// }
17-
//},
183
"AngularClient1": {
194
"commandName": "Project",
205
"launchBrowser": true,
21-
"applicationUrl": "https://localhost:44311;http://localhost:43412",
6+
"applicationUrl": "http://localhost:44311;",
227
"environmentVariables": {
238
"ASPNETCORE_ENVIRONMENT": "Development"
249
}

src/AngularClient/appsettings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
}
99
},
1010
"ClientAppSettings": {
11-
"stsServer": "https://localhost:44318",
12-
"redirect_url": "https://localhost:44311",
11+
"stsServer": "http://localhost:44319",
12+
"redirect_url": "http://localhost:44311",
1313
"client_id": "angularclient",
1414
"response_type": "id_token token",
1515
"scope": "dataEventRecords securedFiles openid profile",
16-
"post_logout_redirect_uri": "https://localhost:44311",
16+
"post_logout_redirect_uri": "http://localhost:44311",
1717
"start_checksession": true,
1818
"silent_renew": true,
1919
"startup_route": "/dataeventrecords",

src/StsServerIdentity/Config.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static IEnumerable<IdentityResource> GetIdentityResources()
1515
{
1616
new IdentityResources.OpenId(),
1717
new IdentityResources.Profile(),
18-
new IdentityResources.Email(),
18+
new IdentityResources.Email(),
1919
new IdentityResource("dataeventrecordsscope",new []{ "role", "admin", "user", "dataEventRecords", "dataEventRecords.admin" , "dataEventRecords.user" } ),
2020
new IdentityResource("securedfilesscope",new []{ "role", "admin", "user", "securedFiles", "securedFiles.admin", "securedFiles.user"} )
2121
};
@@ -61,10 +61,8 @@ public static IEnumerable<ApiResource> GetApiResources()
6161
}
6262

6363
// clients want to access resources (aka scopes)
64-
public static IEnumerable<Client> GetClients(IConfigurationSection stsConfig)
64+
public static IEnumerable<Client> GetClients()
6565
{
66-
var angularClientIdTokenOnlyUrl = stsConfig["AngularClientIdTokenOnlyUrl"];
67-
var angularClientUrl = stsConfig["AngularClientUrl"];
6866
// TODO use configs in app
6967

7068
// client credentials client
@@ -82,18 +80,17 @@ public static IEnumerable<Client> GetClients(IConfigurationSection stsConfig)
8280
AllowAccessTokensViaBrowser = true,
8381
RedirectUris = new List<string>
8482
{
85-
"https://localhost:44311",
86-
"https://localhost:44311/silent-renew.html"
83+
"http://localhost:44311",
84+
"http://localhost:44311/silent-renew.html"
8785

8886
},
8987
PostLogoutRedirectUris = new List<string>
9088
{
91-
"https://localhost:44311/unauthorized",
92-
"https://localhost:44311"
89+
"http://localhost:44311/unauthorized",
90+
"http://localhost:44311"
9391
},
9492
AllowedCorsOrigins = new List<string>
9593
{
96-
"https://localhost:44311",
9794
"http://localhost:44311"
9895
},
9996
AllowedScopes = new List<string>

src/StsServerIdentity/Controllers/AccountController.cs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
using System;
2-
using System.Linq;
3-
using System.Security.Claims;
4-
using System.Threading.Tasks;
1+
using IdentityModel;
2+
using IdentityServer4;
3+
using IdentityServer4.Extensions;
4+
using IdentityServer4.Models;
5+
using IdentityServer4.Services;
6+
using IdentityServer4.Stores;
57
using Microsoft.AspNetCore.Authorization;
68
using Microsoft.AspNetCore.Identity;
79
using Microsoft.AspNetCore.Mvc;
810
using Microsoft.AspNetCore.Mvc.Rendering;
11+
using Microsoft.Extensions.Localization;
912
using Microsoft.Extensions.Logging;
10-
using StsServerIdentity.Models.AccountViewModels;
1113
using StsServerIdentity.Models;
12-
using IdentityServer4.Services;
13-
using IdentityServer4.Stores;
14-
using IdentityServer4.Models;
15-
using IdentityModel;
16-
using IdentityServer4;
17-
using IdentityServer4.Extensions;
18-
using System.Globalization;
19-
using StsServerIdentity.Services;
20-
using Microsoft.Extensions.Localization;
14+
using StsServerIdentity.Models.AccountViewModels;
2115
using StsServerIdentity.Resources;
16+
using StsServerIdentity.Services;
17+
using System;
18+
using System.Globalization;
19+
using System.Linq;
2220
using System.Reflection;
21+
using System.Security.Claims;
22+
using System.Threading.Tasks;
2323

2424
namespace StsServerIdentity.Controllers
2525
{
@@ -275,7 +275,8 @@ public async Task<IActionResult> Register(RegisterViewModel model, string return
275275
{
276276
var user = new ApplicationUser {
277277
UserName = model.Email,
278-
Email = model.Email
278+
Email = model.Email,
279+
//IsAdmin = false
279280
};
280281
var result = await _userManager.CreateAsync(user, model.Password);
281282
if (result.Succeeded)

src/StsServerIdentity/Program.cs

+24-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
using System;
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
25
using Microsoft.AspNetCore;
36
using Microsoft.AspNetCore.Hosting;
7+
48
using Serilog;
59
using Serilog.Events;
610
using Serilog.Sinks.SystemConsole.Themes;
@@ -9,42 +13,28 @@ namespace StsServerIdentity
913
{
1014
public class Program
1115
{
12-
public static int Main(string[] args)
16+
public static void Main(string[] args)
1317
{
14-
Log.Logger = new LoggerConfiguration()
15-
.MinimumLevel.Debug()
16-
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
17-
.Enrich.FromLogContext()
18-
.WriteTo.Console()
19-
.CreateLogger();
18+
var host = CreateWebHostBuilder(args).Build();
2019

21-
try
22-
{
23-
Log.Information("Starting web host");
24-
CreateWebHostBuilder(args).Build().Run();
25-
return 0;
26-
}
27-
catch (Exception ex)
28-
{
29-
Log.Fatal(ex, "Host terminated unexpectedly");
30-
return 1;
31-
}
32-
finally
33-
{
34-
Log.CloseAndFlush();
35-
}
20+
host.Run();
3621
}
3722

38-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
39-
WebHost.CreateDefaultBuilder(args)
40-
.UseStartup<Startup>()
41-
//.UseKestrel(c => c.AddServerHeader = false)
42-
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
43-
.ReadFrom.Configuration(hostingContext.Configuration)
44-
.MinimumLevel.Debug()
45-
.Enrich.FromLogContext()
46-
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
47-
//.WriteTo.RollingFile("Log")
48-
);
23+
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
24+
{
25+
return WebHost.CreateDefaultBuilder(args)
26+
.UseStartup<Startup>()
27+
.UseSerilog((context, configuration) =>
28+
{
29+
configuration
30+
.MinimumLevel.Debug()
31+
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
32+
.MinimumLevel.Override("System", LogEventLevel.Warning)
33+
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
34+
.Enrich.FromLogContext()
35+
.WriteTo.File(@"identityserver4.log")
36+
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate);
37+
});
38+
}
4939
}
5040
}

src/StsServerIdentity/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"StsServerIdentity": {
44
"commandName": "Project",
55
"launchBrowser": true,
6-
"applicationUrl": "https://localhost:44318;http://localhost:44319",
6+
"applicationUrl": "http://localhost:44319",
77
"environmentVariables": {
88
"ASPNETCORE_ENVIRONMENT": "Development"
99
}

src/StsServerIdentity/SeedData.cs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
5+
using System;
6+
using System.Linq;
7+
using System.Security.Claims;
8+
9+
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.Extensions.DependencyInjection;
12+
13+
using StsServerIdentity.Data;
14+
using StsServerIdentity.Models;
15+
16+
using IdentityModel;
17+
18+
namespace StsServerIdentity
19+
{
20+
public class SeedData
21+
{
22+
public static void EnsureSeedData(IServiceProvider serviceProvider)
23+
{
24+
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
25+
{
26+
var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
27+
context.Database.Migrate();
28+
29+
var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
30+
var alice = userMgr.FindByNameAsync("alice").Result;
31+
if (alice == null)
32+
{
33+
alice = new ApplicationUser
34+
{
35+
UserName = "alice"
36+
};
37+
var result = userMgr.CreateAsync(alice, "Pass123$").Result;
38+
if (!result.Succeeded)
39+
{
40+
throw new Exception(result.Errors.First().Description);
41+
}
42+
43+
result = userMgr.AddClaimsAsync(alice, new Claim[]{
44+
new Claim(JwtClaimTypes.Name, "Alice Smith"),
45+
new Claim(JwtClaimTypes.GivenName, "Alice"),
46+
new Claim(JwtClaimTypes.FamilyName, "Smith"),
47+
new Claim(JwtClaimTypes.Email, "[email protected]"),
48+
new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
49+
new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
50+
new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json)
51+
}).Result;
52+
if (!result.Succeeded)
53+
{
54+
throw new Exception(result.Errors.First().Description);
55+
}
56+
Console.WriteLine("alice created");
57+
}
58+
else
59+
{
60+
Console.WriteLine("alice already exists");
61+
}
62+
63+
var bob = userMgr.FindByNameAsync("bob").Result;
64+
if (bob == null)
65+
{
66+
bob = new ApplicationUser
67+
{
68+
UserName = "bob"
69+
};
70+
var result = userMgr.CreateAsync(bob, "Pass123$").Result;
71+
if (!result.Succeeded)
72+
{
73+
throw new Exception(result.Errors.First().Description);
74+
}
75+
76+
result = userMgr.AddClaimsAsync(bob, new Claim[]{
77+
new Claim(JwtClaimTypes.Name, "Bob Smith"),
78+
new Claim(JwtClaimTypes.GivenName, "Bob"),
79+
new Claim(JwtClaimTypes.FamilyName, "Smith"),
80+
new Claim(JwtClaimTypes.Email, "[email protected]"),
81+
new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
82+
new Claim(JwtClaimTypes.WebSite, "http://bob.com"),
83+
new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),
84+
new Claim("location", "somewhere")
85+
}).Result;
86+
if (!result.Succeeded)
87+
{
88+
throw new Exception(result.Errors.First().Description);
89+
}
90+
Console.WriteLine("bob created");
91+
}
92+
else
93+
{
94+
Console.WriteLine("bob already exists");
95+
}
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)