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