Bearer token request using Duende.IdentityModel.Client¶
In [ ]:
Copied!
#!csharp
#load "../HttpLoggerHelper.csx"
#r "nuget: Fhi.Authentication.Extensions, 1.0.0-beta"
#r "nuget: Duende.IdentityModel, 7.1.0"
using System.Net.Http;
using System.Net.Http.Headers;
using Duende.IdentityModel.Client;
using Duende.IdentityModel;
var client = new HttpClient();
var discovery = await client.GetDiscoveryDocumentAsync("https://demo.duendesoftware.com");
Console.WriteLine($"Discovery Document: {discovery.IsError}");
if (discovery is not null && !discovery.IsError && discovery.Issuer is not null && discovery.TokenEndpoint is not null)
{
var tokenRequest = new ClientCredentialsTokenRequest()
{
ClientId = "m2m",
Address = discovery.TokenEndpoint,
GrantType = OidcConstants.GrantTypes.ClientCredentials,
ClientCredentialStyle = ClientCredentialStyle.PostBody,
ClientSecret = "secret", // This is used for shared secrets, not needed when using client assertion
////ClientAssertion = new ClientAssertion()
////{
//// Type = OidcConstants.ClientAssertionTypes.JwtBearer,
//// Value = ClientAssertionTokenHandler.CreateJwtToken(discovery.Issuer, ""m2m.jwt, "jwk...")
////},
Scope = "api"
};
var tokenResponse = await client.RequestClientCredentialsTokenAsync(tokenRequest);
await HttpLogger.LogResponse(tokenResponse.HttpResponse);
}
#!csharp
#load "../HttpLoggerHelper.csx"
#r "nuget: Fhi.Authentication.Extensions, 1.0.0-beta"
#r "nuget: Duende.IdentityModel, 7.1.0"
using System.Net.Http;
using System.Net.Http.Headers;
using Duende.IdentityModel.Client;
using Duende.IdentityModel;
var client = new HttpClient();
var discovery = await client.GetDiscoveryDocumentAsync("https://demo.duendesoftware.com");
Console.WriteLine($"Discovery Document: {discovery.IsError}");
if (discovery is not null && !discovery.IsError && discovery.Issuer is not null && discovery.TokenEndpoint is not null)
{
var tokenRequest = new ClientCredentialsTokenRequest()
{
ClientId = "m2m",
Address = discovery.TokenEndpoint,
GrantType = OidcConstants.GrantTypes.ClientCredentials,
ClientCredentialStyle = ClientCredentialStyle.PostBody,
ClientSecret = "secret", // This is used for shared secrets, not needed when using client assertion
////ClientAssertion = new ClientAssertion()
////{
//// Type = OidcConstants.ClientAssertionTypes.JwtBearer,
//// Value = ClientAssertionTokenHandler.CreateJwtToken(discovery.Issuer, ""m2m.jwt, "jwk...")
////},
Scope = "api"
};
var tokenResponse = await client.RequestClientCredentialsTokenAsync(tokenRequest);
await HttpLogger.LogResponse(tokenResponse.HttpResponse);
}