While updating my training material about OIDC I noticed that the documentation of IdentityServer didn’t reflect the latest changes in the IdentityModel library.
The example found in the documentation was still using the old TokenClient together with a RequestCustomGrantAsync() method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<TokenResponse> DelegateAsync(string userToken) | |
{ | |
var payload = new | |
{ | |
token = userToken | |
}; | |
// create token client | |
var client = new TokenClient(disco.TokenEndpoint, "api1.client", "secret"); | |
// send custom grant to token endpoint, return response | |
return await client.RequestCustomGrantAsync("delegation", "api2", payload); | |
} |
This method no longer exists in version 4 of IdentityModel. Instead you need to use the RequestTokenAsync() extension method on the HttpClient:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<TokenResponse> DelegateAsync(string userToken) | |
{ | |
// create client | |
var client = _clientFactory.CreateClient(); | |
var disco = await client.GetDiscoveryDocumentAsync("https://localhost:44303/"); | |
// send custom grant to token endpoint, return response | |
return await client.RequestTokenAsync(new TokenRequest | |
{ | |
Address = disco.TokenEndpoint, | |
GrantType = "delegation", | |
ClientId = "api1.client", | |
ClientSecret = "secret", | |
Parameters = | |
{ | |
{ "scope", "api2" }, | |
{ "token", userToken} | |
}, | |
}); | |
} |