Skip to content

Commit 83b404a

Browse files
demo.duende.com
1 parent 98330c2 commit 83b404a

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

access-token-management/samples/WebClientAssertions/Controllers/HomeController.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using System.Text.Json;
55
using Duende.AccessTokenManagement;
6+
using Duende.AccessTokenManagement.DPoP;
67
using Duende.AccessTokenManagement.OpenIdConnect;
7-
using Duende.IdentityModel.Client;
88
using Microsoft.AspNetCore.Authentication;
99
using Microsoft.AspNetCore.Authorization;
1010
using Microsoft.AspNetCore.Mvc;
@@ -15,11 +15,13 @@ public class HomeController : Controller
1515
{
1616
private readonly IHttpClientFactory _httpClientFactory;
1717
private readonly IUserTokenManager _tokenManager;
18+
private readonly IDPoPProofService _dPoPProofService;
1819

19-
public HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager tokenManager)
20+
public HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager tokenManager, IDPoPProofService dPoPProofService)
2021
{
2122
_httpClientFactory = httpClientFactory;
2223
_tokenManager = tokenManager;
24+
_dPoPProofService = dPoPProofService;
2325
}
2426

2527
[AllowAnonymous]
@@ -39,11 +41,33 @@ public HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager to
3941
public async Task<IActionResult> CallApiAsUserManual()
4042
{
4143
var token = await _tokenManager.GetAccessTokenAsync(User).GetToken();
44+
45+
var url = new Uri("https://demo.duendesoftware.com/api/dpop/test");
46+
var request = new HttpRequestMessage(HttpMethod.Get, url);
47+
request.Headers.Authorization = new ("DPoP", token.AccessToken.ToString());
48+
49+
if (token.DPoPJsonWebKey is { } key)
50+
{
51+
var proof = await _dPoPProofService.CreateProofTokenAsync(new DPoPProofRequest
52+
{
53+
Url = url,
54+
Method = HttpMethod.Get,
55+
DPoPProofKey = key,
56+
AccessToken = token.AccessToken,
57+
});
58+
59+
if (proof is not null)
60+
{
61+
request.SetDPoPProofToken(proof.Value);
62+
}
63+
}
64+
4265
var client = _httpClientFactory.CreateClient();
43-
client.SetBearerToken(token.AccessToken.ToString()!);
66+
var response = await client.SendAsync(request);
67+
response.EnsureSuccessStatusCode();
4468

45-
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");
46-
ViewBag.Json = PrettyPrint(response);
69+
var json = await response.Content.ReadAsStringAsync();
70+
ViewBag.Json = PrettyPrint(json);
4771

4872
return View("CallApi");
4973
}

access-token-management/samples/WebClientAssertions/Startup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
8686
// Register our client assertion service (replaces the default no-op)
8787
builder.Services.AddTransient<IClientAssertionService, ClientAssertionService>();
8888

89-
// --- Named M2M client (client credentials with JWT auth, no DPoP) ---
89+
// --- Named M2M client (client credentials with JWT auth + DPoP) ---
9090
builder.Services.AddClientCredentialsTokenManagement()
9191
.AddClient("m2m.jwt", client =>
9292
{
9393
client.TokenEndpoint = new Uri("https://demo.duendesoftware.com/connect/token");
9494
client.ClientId = ClientId.Parse("m2m.jwt");
9595
// No ClientSecret — assertion service provides credentials
9696
client.Scope = Scope.Parse("api");
97+
client.DPoPJsonWebKey = DPoPProofKey.Parse(dpopJwk);
9798
});
9899

99100
// --- HTTP Clients ---
@@ -111,17 +112,17 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
111112
})
112113
.AddUserAccessTokenHandler();
113114

114-
// Client access token clients (M2M with JWT assertion, no DPoP)
115+
// Client access token clients (M2M with JWT assertion + DPoP)
115116
builder.Services.AddClientCredentialsHttpClient("client",
116117
ClientCredentialsClientName.Parse("m2m.jwt"),
117118
client =>
118119
{
119-
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
120+
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
120121
});
121122

122123
builder.Services.AddHttpClient<TypedClientClient>(client =>
123124
{
124-
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
125+
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
125126
})
126127
.AddClientCredentialsTokenHandler(ClientCredentialsClientName.Parse("m2m.jwt"));
127128

0 commit comments

Comments
 (0)