5
5
using System . Security . Cryptography ;
6
6
using System . Text . Json ;
7
7
using System . Threading . Tasks ;
8
+ using Duende . AccessTokenManagement . OpenIdConnect ;
8
9
using Microsoft . AspNetCore . Authentication ;
9
10
using Microsoft . AspNetCore . Builder ;
10
11
using Microsoft . Extensions . DependencyInjection ;
@@ -16,11 +17,14 @@ namespace Web;
16
17
17
18
public static class Startup
18
19
{
19
- public const bool UseDPoP = false ;
20
+ public const bool UseDPoP = true ;
21
+
22
+ public const string BaseUrl = "https://localhost:5001" ;
23
+ //public const string BaseUrl = "https://demo.duendesoftware.com";
20
24
21
25
public const string ApiBaseUrl = UseDPoP ?
22
- "https://demo.duendesoftware.com /api/dpop/" :
23
- "https://demo.duendesoftware.com /api/";
26
+ $ " { BaseUrl } /api/dpop/" :
27
+ $ " { BaseUrl } /api/";
24
28
25
29
internal static WebApplication ConfigureServices ( this WebApplicationBuilder builder )
26
30
{
@@ -39,8 +43,7 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
39
43
} )
40
44
. AddOpenIdConnect ( "oidc" , options =>
41
45
{
42
- options . Authority = "https://demo.duendesoftware.com" ;
43
- //options.Authority = "https://localhost:5001";
46
+ options . Authority = BaseUrl ;
44
47
45
48
options . ClientId = "interactive.confidential.short" ;
46
49
options . ClientSecret = "secret" ;
@@ -56,6 +59,8 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
56
59
options . Scope . Add ( "api" ) ;
57
60
options . Scope . Add ( "resource1.scope1" ) ;
58
61
62
+ options . Resource = "urn:resource1" ;
63
+
59
64
options . GetClaimsFromUserInfoEndpoint = true ;
60
65
options . SaveTokens = true ;
61
66
options . MapInboundClaims = false ;
@@ -65,12 +70,6 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
65
70
NameClaimType = "name" ,
66
71
RoleClaimType = "role"
67
72
} ;
68
-
69
- options . Events . OnRedirectToIdentityProvider = ctx =>
70
- {
71
- ctx . ProtocolMessage . Resource = "urn:resource1" ;
72
- return Task . CompletedTask ;
73
- } ;
74
73
} ) ;
75
74
76
75
var rsaKey = new RsaSecurityKey ( RSA . Create ( 2048 ) ) ;
@@ -80,11 +79,22 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
80
79
81
80
builder . Services . AddOpenIdConnectAccessTokenManagement ( options =>
82
81
{
83
- options . DPoPJsonWebKey = UseDPoP ? jwk : null ; ;
82
+ options . DPoPJsonWebKey = UseDPoP ? jwk : null ;
84
83
} ) ;
85
84
86
85
// registers HTTP client that uses the managed user access token
87
- builder . Services . AddUserAccessTokenHttpClient ( "user_client" ,
86
+ builder . Services . AddUserAccessTokenHttpClient ( "user" ,
87
+ configureClient : client => {
88
+ client . BaseAddress = new Uri ( ApiBaseUrl ) ;
89
+ } ) ;
90
+
91
+ // registers HTTP client that uses the managed user access token and
92
+ // includes a resource indicator
93
+ builder . Services . AddUserAccessTokenHttpClient ( "user-resource" ,
94
+ new UserTokenRequestParameters
95
+ {
96
+ Resource = "urn:resource1"
97
+ } ,
88
98
configureClient : client => {
89
99
client . BaseAddress = new Uri ( ApiBaseUrl ) ;
90
100
} ) ;
@@ -93,6 +103,15 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
93
103
builder . Services . AddClientAccessTokenHttpClient ( "client" ,
94
104
configureClient : client => { client . BaseAddress = new Uri ( ApiBaseUrl ) ; } ) ;
95
105
106
+ // registers HTTP client that uses the managed client access token and
107
+ // includes a resource indicator
108
+ builder . Services . AddClientAccessTokenHttpClient ( "client-resource" ,
109
+ new UserTokenRequestParameters
110
+ {
111
+ Resource = "urn:resource1"
112
+ } ,
113
+ configureClient : client => { client . BaseAddress = new Uri ( ApiBaseUrl ) ; } ) ;
114
+
96
115
// registers a typed HTTP client with token management support
97
116
builder . Services . AddHttpClient < TypedUserClient > ( client =>
98
117
{
0 commit comments