1+ using Microsoft . IdentityModel . Tokens ;
2+ using SimpleIdServer . IdServer . Domains ;
3+ using SimpleIdServer . OpenidFederation . Domains ;
4+ using SimpleIdServer . OpenidFederation . Store . EF ;
5+ using System . Security . Cryptography ;
6+
7+ var builder = WebApplication . CreateBuilder ( args ) ;
8+ var signatureCredentials = new SigningCredentials ( new RsaSecurityKey ( RSA . Create ( ) ) { KeyId = "raId" } , SecurityAlgorithms . RsaSha256 ) ;
9+
10+ var jsonWebKey = signatureCredentials . SerializePublicJWK ( ) ;
11+ jsonWebKey . Alg = SecurityAlgorithms . RsaSha256 ;
12+ jsonWebKey . Use = "sig" ;
13+
14+ builder . Services . AddDistributedMemoryCache ( ) ;
15+ builder . Services . AddRpFederation ( r =>
16+ {
17+ r . Client = new SimpleIdServer . IdServer . Domains . Client
18+ {
19+ ClientId = "http://localhost:7001" ,
20+ RedirectionUrls = new List < string >
21+ {
22+ "http://localhost:7001/signin-oidc"
23+ } ,
24+ ClientRegistrationTypesSupported = new List < string >
25+ {
26+ "automatic"
27+ } ,
28+ RequestObjectSigningAlg = SecurityAlgorithms . RsaSha256 ,
29+ Scopes = new List < Scope >
30+ {
31+ new Scope
32+ {
33+ Name = "openid"
34+ } ,
35+ new Scope
36+ {
37+ Name = "profile"
38+ }
39+ } ,
40+ ResponseTypes = new List < string >
41+ {
42+ "code"
43+ } ,
44+ GrantTypes = new List < string >
45+ {
46+ "authorization_code"
47+ } ,
48+ TokenEndPointAuthMethod = "private_key_jwt"
49+ } ;
50+ r . Client . Add ( jsonWebKey . Kid , jsonWebKey , "sig" , SecurityKeyTypes . RSA ) ;
51+ r . SigningCredentials = signatureCredentials ;
52+ } ) ;
53+ builder . Services . AddOpenidFederationStore ( ) ;
54+ builder . Services . AddControllersWithViews ( ) ;
55+ builder . Services . AddAuthentication ( options =>
56+ {
57+ options . DefaultScheme = "Cookies" ;
58+ options . DefaultChallengeScheme = "sid" ;
59+ } )
60+ . AddCookie ( "Cookies" )
61+ . AddCustomOpenIdConnect ( "sid" , options =>
62+ {
63+
64+ options . SignInScheme = "Cookies" ;
65+ options . ResponseType = "code" ;
66+ options . Authority = "https://localhost:5001/master" ;
67+ options . RequireHttpsMetadata = false ;
68+ options . ClientId = "http://localhost:7001" ;
69+ options . GetClaimsFromUserInfoEndpoint = true ;
70+ options . SaveTokens = true ;
71+ options . UseFederationAutomaticRegistration ( signatureCredentials ) ;
72+ } ) ;
73+
74+ var app = builder . Build ( ) ;
75+ AddTrustedEntities ( app . Services ) ;
76+ app . UseHttpsRedirection ( ) ;
77+ app . UseStaticFiles ( ) ;
78+ app . UseRouting ( ) ;
79+ app . UseAuthorization ( ) ;
80+ app . MapControllerRoute (
81+ name : "default" ,
82+ pattern : "{controller=Home}/{action=Index}/{id?}" ) ;
83+
84+ app . Run ( ) ;
85+ static void AddTrustedEntities ( IServiceProvider services )
86+ {
87+ using ( var scope = services . CreateScope ( ) )
88+ {
89+ var dbContext = scope . ServiceProvider . GetRequiredService < OpenidFederationDbContext > ( ) ;
90+ dbContext . FederationEntities . AddRange ( new List < FederationEntity >
91+ {
92+ new FederationEntity
93+ {
94+ Id = Guid . NewGuid ( ) . ToString ( ) ,
95+ Sub = "http://localhost:7000" ,
96+ Realm = string . Empty ,
97+ IsSubordinate = false
98+ }
99+ } ) ;
100+ dbContext . SaveChanges ( ) ;
101+ }
102+ }
0 commit comments