Fhi.ClientCredentialsKeypairs
1.4.0
See the version list below for details.
dotnet add package Fhi.ClientCredentialsKeypairs --version 1.4.0
NuGet\Install-Package Fhi.ClientCredentialsKeypairs -Version 1.4.0
<PackageReference Include="Fhi.ClientCredentialsKeypairs" Version="1.4.0" />
paket add Fhi.ClientCredentialsKeypairs --version 1.4.0
#r "nuget: Fhi.ClientCredentialsKeypairs, 1.4.0"
// Install Fhi.ClientCredentialsKeypairs as a Cake Addin #addin nuget:?package=Fhi.ClientCredentialsKeypairs&version=1.4.0 // Install Fhi.ClientCredentialsKeypairs as a Cake Tool #tool nuget:?package=Fhi.ClientCredentialsKeypairs&version=1.4.0
Client Credentials Usage
Configuration file section
- Add the following configuration section to your appsettings.json files, and populate it appropriately.
"ClientCredentialsConfiguration": {
"clientName": "",
"authority": "",
"clientId": "",
"grantTypes": [ "client_credentials" ],
"scopes": [ ],
"secretType": "private_key_jwt:RsaPrivateKeyJwtSecret",
"rsaPrivateKey": "",
"rsaKeySizeBits": 4096,
"privateJwk": "",
"Apis": [
{
"Name": "", // Tip: Use nameof(YourService)
"Url": ""
}
],
"refreshTokenAfterMinutes": 8 // Set approx 20% less than lifetime of access token
}
PS: Please be aware that the Authority must end with connect/token
.
Client Credentials using Keypairs
Add package 'Fhi.ClientCredentialsKeypairs' to your project
In your
Program.cs
file, or if olderStartup.cs
, add the following code section (for the outgoing interfaces):
var clientCredentialsConfiguration = services.AddClientCredentialsKeypairs(Configuration);
services.AddHttpClient(nameof(YourService), c =>
{
c.Timeout = new TimeSpan(0, 0, 0, 10);
c.BaseAddress = clientCredentialsConfiguration.UriToApiByName(nameof(YourService));
})
.AddHttpMessageHandler<HttpAuthHandler>()
.AddTypedClient(c => RestService.For<IExternalApi>(c, new RefitSettings
{
ContentSerializer = new SystemTextJsonContentSerializer(services.DefaultJsonSerializationOptions())
}));
replacing YourService
with the service you have done for accessing the external api, and replace IExternalApi
with the Refit interface for whatever external api you want to access.
For usages of Refit that uses an interface (in this example IMyService
is the interface that Refit will implement), the code would look something like this:
services
.AddRefitClient<IMyService>()
.ConfigureHttpClient(c =>
{
c.BaseAddress = clientCredentialsConfiguration.UriToApiByName(nameof(IMyService));
})
.AddHttpMessageHandler<HttpAuthHandler>();
The Configuration
property is the injected IConfiguration property from the Startup.cs file.
If you don't use Refit, you can just skip the last part, and get the named client from the injected HttpFactory in your service. It will still have the authenticationhandler, so you don't need to do anything more there to get the bearer token. It will be added automatically.
Client Credentials using Client Secrets
If you want to disable the authorization for some reason, you can add another property named Enable
to the ClientCredentialsConfiguration, it is default true.
- Add package
Fhi.ClientCredentialsUsingSecrets
to your project - In your
Program.cs
file, create an instance of theClientCredentialsSetup
class using anIConfiguration
parameter. - Using the created instance call the method
ConfigureServices
.
Calling endpoints that does not required authentication
In some cases we might wish to call an API before we are authenticated (health endpoints, kodeverk, etc..).
To make the HttpAuthHandler not add authentication headers to a single request you can add an Option to the request with the key name "Anonymous":
var request = new HttpRequestMessage();
request.Options.TryAdd("Anonymous", "");
or in Refit:
[Get("/info")]
Task<string> GetInfo([Property("Anonymous")] string anonymous = "");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- IdentityModel (>= 6.2.0)
- IdentityModel.AspNetCore (>= 4.3.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Fhi.ClientCredentialsKeypairs:
Package | Downloads |
---|---|
Fhi.ClientCredentials.TestSupport
This package contains tests that checks your appsettings.*.json files for correct configuration of the ClientCredentials package. |
|
Fhi.ClientCredentials.Refit
Builder to simplify setup of ClientCredentials when using the Refit library. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0 | 179 | 11/26/2024 |
3.0.0-beta2 | 53 | 11/26/2024 |
2.0.3 | 2,271 | 10/7/2024 |
2.0.2 | 324 | 10/2/2024 |
2.0.0 | 123 | 10/1/2024 |
1.4.1 | 334 | 9/23/2024 |
1.4.0 | 107 | 9/20/2024 |
1.2.0 | 11,189 | 8/19/2023 |
1.1.0 | 1,559 | 8/9/2023 |
1.0.0 | 5,243 | 2/16/2023 |
1.0.0-beta.5 | 119 | 2/15/2023 |
1.0.0-beta.4 | 97 | 2/15/2023 |
1.0.0-beta.3 | 96 | 2/15/2023 |
1.0.0-beta.2 | 101 | 2/14/2023 |
1.0.0-beta.1 | 343 | 11/15/2022 |
1.0.0-alpha.4 | 187 | 11/4/2022 |