Skip to content

Commit 92e5b7d

Browse files
committed
Added test host with IdentityServer4
1 parent bb189dd commit 92e5b7d

5 files changed

Lines changed: 172 additions & 0 deletions

File tree

IdentityManager2.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hosts.Shared", "src\Hosts\H
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Hosts", "Hosts", "{59FA21EB-3472-4E3D-BDF0-AD32DCFA6035}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.IdentityServerAuthentication", "src\Hosts\Host.IdentityServerAuthentication\Hosts.IdentityServerAuthentication.csproj", "{BAACD397-A69F-4F9B-A178-39D734CA78CC}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -37,6 +39,10 @@ Global
3739
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{BAACD397-A69F-4F9B-A178-39D734CA78CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{BAACD397-A69F-4F9B-A178-39D734CA78CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{BAACD397-A69F-4F9B-A178-39D734CA78CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{BAACD397-A69F-4F9B-A178-39D734CA78CC}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
GlobalSection(SolutionProperties) = preSolution
4248
HideSolutionNode = FALSE
@@ -47,6 +53,7 @@ Global
4753
{20FEF10D-D003-48A7-A931-4DEB80796E08} = {59FA21EB-3472-4E3D-BDF0-AD32DCFA6035}
4854
{BE7A2243-B7D0-4D32-92F6-0DD4C8DA8208} = {59FA21EB-3472-4E3D-BDF0-AD32DCFA6035}
4955
{59FA21EB-3472-4E3D-BDF0-AD32DCFA6035} = {21035206-B373-4994-901B-2C9E882B5852}
56+
{BAACD397-A69F-4F9B-A178-39D734CA78CC} = {59FA21EB-3472-4E3D-BDF0-AD32DCFA6035}
5057
EndGlobalSection
5158
GlobalSection(ExtensibilityGlobals) = postSolution
5259
SolutionGuid = {22A3DD5E-832A-4FFC-B0B9-1A3D07313154}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="IdentityServer4" Version="3.1.0-preview.1.4" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Hosts.Shared\Hosts.Shared.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace Hosts.IdentityServerAuthentication
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Host.IdentityServerAuthentication": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"applicationUrl": "http://localhost:5000",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Hosts.Shared.InMemory;
7+
using IdentityManager2.Configuration;
8+
using IdentityServer4;
9+
using IdentityServer4.Models;
10+
using IdentityServer4.Test;
11+
using Microsoft.AspNetCore.Authentication;
12+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
13+
using Microsoft.AspNetCore.Builder;
14+
using Microsoft.AspNetCore.Routing;
15+
using Microsoft.Extensions.DependencyInjection;
16+
17+
namespace Hosts.IdentityServerAuthentication
18+
{
19+
public class Startup
20+
{
21+
public void ConfigureServices(IServiceCollection services)
22+
{
23+
// In-memory IdentityManagerService (demo only)
24+
services.AddIdentityManager(opt =>
25+
opt.SecurityConfiguration =
26+
new SecurityConfiguration
27+
{
28+
HostAuthenticationType = "cookie",
29+
HostChallengeType = "oidc"
30+
})
31+
.AddIdentityMangerService<InMemoryIdentityManagerService>();
32+
33+
var admin = new TestUser
34+
{
35+
SubjectId = "123",
36+
Username = "scott",
37+
Password = "scott",
38+
Claims = {new Claim("role", "IdentityManagerAdministrator")}
39+
};
40+
41+
var client = new Client
42+
{
43+
ClientId = "identitymanager2",
44+
ClientName = "IdentityManager2",
45+
AllowedGrantTypes = GrantTypes.Implicit,
46+
RedirectUris = {"http://localhost:5000/idm/signin-oidc"},
47+
AllowedScopes = {"openid", "profile", "roles"},
48+
RequireConsent = false
49+
};
50+
51+
var roles = new IdentityResource("roles", new List<string> {"role"});
52+
53+
services.AddIdentityServer()
54+
.AddTestUsers(new List<TestUser> {admin})
55+
.AddInMemoryIdentityResources(new List<IdentityResource> {new IdentityResources.OpenId(), new IdentityResources.Profile(), roles})
56+
.AddInMemoryApiResources(new List<ApiResource>())
57+
.AddInMemoryClients(new List<Client> {client})
58+
.AddDeveloperSigningCredential(false);
59+
60+
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
61+
62+
services.AddAuthentication()
63+
.AddCookie("cookie")
64+
.AddOpenIdConnect("oidc", opt =>
65+
{
66+
opt.Authority = "http://localhost:5000/auth";
67+
opt.ClientId = "identitymanager2";
68+
69+
// default: openid & profile
70+
opt.Scope.Add("roles");
71+
72+
opt.RequireHttpsMetadata = false; // dev only
73+
opt.SignInScheme = "cookie";
74+
opt.CallbackPath = "/signin-oidc";
75+
76+
opt.Events = new OpenIdConnectEvents
77+
{
78+
OnTokenValidated = context => Task.CompletedTask
79+
};
80+
});
81+
82+
var rand = new Random();
83+
services.AddSingleton(x => Users.Get(rand.Next(5000, 20000)));
84+
services.AddSingleton(x => Roles.Get(rand.Next(15)));
85+
}
86+
87+
public void Configure(IApplicationBuilder app)
88+
{
89+
app.UseDeveloperExceptionPage();
90+
91+
app.Map("/auth", auth =>
92+
{
93+
auth.UseIdentityServer();
94+
95+
// Force authentication
96+
auth.Map("/account/login",
97+
login => login.Use(async (context, func) =>
98+
{
99+
await context.SignInAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme,
100+
new ClaimsPrincipal(new ClaimsIdentity(new List<Claim> {new Claim("sub", "123")}, IdentityServerConstants.DefaultCookieAuthenticationScheme)));
101+
context.Response.Redirect(context.Request.Query["returnUrl"]);
102+
}));
103+
});
104+
105+
app.Map("/idm", idm =>
106+
{
107+
idm.UseRouting();
108+
109+
idm.UseAuthentication();
110+
idm.UseAuthorization();
111+
112+
idm.UseIdentityManager();
113+
114+
idm.UseEndpoints(x =>
115+
{
116+
x.MapDefaultControllerRoute();
117+
});
118+
});
119+
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)