Skip to content

Commit befb96d

Browse files
committed
Improved exception messages.
Updated README.md
1 parent 44f3795 commit befb96d

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

Http/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ AppCore .NET add-ons for Microsoft.Extensions.Http
33

44
This project contains add-ons for the `Microsoft.Extensions.Http` library. It adds authentication support to the `IHttpCLientFactory`.
55

6+
## Features
7+
8+
69
## Packages
710

811
Latest development packages can be found on [MyGet](https://www.myget.org/gallery/appcorenet).
@@ -12,3 +15,44 @@ Latest development packages can be found on [MyGet](https://www.myget.org/galler
1215
| `AppCore.Extensions.Http.Authentication` | Provides extensions which adds support for authenticating a HttpClient using different authentication standards. |
1316
| `AppCore.Extensions.Http.Authentication.OAuth` | Adds support for authenticating a HttpClient using OAuth2 bearer tokens. |
1417
| `AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect` | Adds support for deriving token client configuration from ASP.NET Core OpenID connect authentication schemes. |
18+
19+
## Usage
20+
21+
To add transparent authentication to a `HttpClient` which is built using the `IHttpClientFactory` you first have to register a client authentication scheme.
22+
Currently OAuth2 client credentials and password flow is supported out-of-the-box. Custom schemes can be easily added.
23+
24+
After registering the scheme you have to configure the `IHttpClientFactory` to use the authentication handler. That's all.
25+
26+
Add the package to your project:
27+
28+
```shell
29+
> dotnet add package AppCore.Extensions.Http.Authentication.OAuth
30+
```
31+
32+
Register a client authentication scheme, the following sample uses client credential authentication.
33+
34+
```csharp
35+
services.AddHttpClientAuthentication()
36+
.AddOAuthClient("catalog.client",
37+
o => {
38+
o.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";
39+
o.ClientId = "6f59b670-990f-4ef7-856f-0dd584ed1fac";
40+
o.ClientSecret = "d0c17c6a-ba47-4654-a874-f6d576cdf799";
41+
o.Scope = "catalog inventory";
42+
});
43+
```
44+
45+
Configure your HTTP client to use the authentication scheme:
46+
47+
```csharp
48+
services.AddHttpClient("CatalogClient")
49+
.AddOAuthClientAuthentication("catalog.client");
50+
```
51+
52+
For frontend apps it is also possible to infer the OAuth client configuration from the ASP.Net Core
53+
OIDC authentication scheme.
54+
55+
```csharp
56+
services.AddHttpClientAuthentication()
57+
.AddOAuthClientForScheme(o => { o.OpenIdConnect() });
58+
```

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthAccessToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace AppCore.Extensions.Http.Authentication.OAuth;
99
/// <summary>
1010
/// Represents a OAuth access token.
1111
/// </summary>
12-
public class OAuthAccessToken
12+
public sealed class OAuthAccessToken
1313
{
1414
/// <summary>
1515
/// Gets the access token.

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthOptionsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public virtual async Task<T> GetOptionsAsync<T>(AuthenticationScheme scheme)
4343
if (result == null)
4444
{
4545
throw new InvalidOperationException(
46-
$"No options of type {typeof(T)} could be resolved for client authentication scheme {scheme.Name}.");
46+
$"No options of type '{typeof(T)}' could be resolved for client authentication scheme {scheme.Name}.");
4747
}
4848

4949
return result;

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthOptionsValidator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ protected List<string> Validate(string name, OAuthOptions options)
1313

1414
if (options.TokenEndpoint == null)
1515
{
16-
errors.Add($"Token endpoint for OAuth authentication scheme {name} must not be null.");
16+
errors.Add($"Token endpoint for OAuth authentication scheme '{name}' must not be null.");
1717
}
1818

1919
if (string.IsNullOrWhiteSpace(options.ClientId))
2020
{
21-
errors.Add($"ClientId for OAuth authentication scheme {name} must not be null or empty.");
21+
errors.Add($"ClientId for OAuth authentication scheme '{name}' must not be null or empty.");
2222
}
2323

2424
if (string.IsNullOrWhiteSpace(options.ClientSecret))
2525
{
26-
errors.Add($"ClientSecret for OAuth authentication scheme {name} must not be null or empty.");
26+
errors.Add($"ClientSecret for OAuth authentication scheme '{name}' must not be null or empty.");
2727
}
2828

2929
return errors;

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthTokenCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ await _cache.GetAsync(cacheKey, cancellationToken)
117117

118118
if (result != null)
119119
{
120-
_logger.LogDebug("Cache hit for access token for scheme: {schemeName}", scheme.Name);
120+
_logger.LogTrace("Cache hit for access token for scheme: {schemeName}", scheme.Name);
121121
}
122122
else
123123
{
124-
_logger.LogDebug("Cache miss for access token for scheme: {schemeName}", scheme.Name);
124+
_logger.LogTrace("Cache miss for access token for scheme: {schemeName}", scheme.Name);
125125
}
126126

127127
return result;

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthTokenService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ await _client.RequestClientAccessToken(scheme, parameters, cancellationToken)
9595
response.ErrorDescription);
9696

9797
throw new AuthenticationException(
98-
$"Error requesting access token for client scheme {scheme.Name}");
98+
$"Error requesting access token for client scheme '{scheme.Name}'");
9999
}
100100

101101
OAuthAccessToken token = new(
@@ -158,7 +158,7 @@ await _client.RequestPasswordAccessToken(scheme, parameters, cancellationToken)
158158
response.ErrorDescription);
159159

160160
throw new AuthenticationException(
161-
$"Error requesting access token for password scheme {scheme.Name}");
161+
$"Error requesting access token for password scheme '{scheme.Name}'");
162162
}
163163

164164
OAuthAccessToken token = new(

Http/src/AppCore.Extensions.Http.Authentication/AuthenticationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ await _schemes.FindSchemeAsync(_scheme)
6262

6363
if (authenticationScheme == null)
6464
{
65-
throw new InvalidOperationException($"There is no client authentication scheme registered with name {_scheme}.");
65+
throw new InvalidOperationException($"There is no client authentication scheme registered with name '{_scheme}'.");
6666
}
6767

6868
if (authenticationScheme.HandlerType != typeof(THandler))
6969
{
7070
throw new InvalidOperationException(
71-
$"There is no client authentication scheme registered with name {_scheme} and handler {typeof(THandler).Name}.");
71+
$"There is no client authentication scheme registered with name '{_scheme}' and handler '{typeof(THandler).Name}'.");
7272
}
7373

7474
return authenticationScheme;
@@ -82,7 +82,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
8282
AuthenticationScheme scheme = await GetSchemeAsync()
8383
.ConfigureAwait(false);
8484

85-
_logger.LogDebug("Authenticating HTTP request with scheme {schemeName}.", scheme.Name);
85+
_logger.LogTrace("Authenticating HTTP request with scheme {schemeName}.", scheme.Name);
8686

8787
await AuthenticateAsync(scheme, request, forceRenewal: false, cancellationToken)
8888
.ConfigureAwait(false);

Http/src/AppCore.Extensions.Http.Authentication/AuthenticationScheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace AppCore.Extensions.Http.Authentication;
99
/// <summary>
1010
/// Represents a registered authentication scheme.
1111
/// </summary>
12-
public class AuthenticationScheme
12+
public sealed class AuthenticationScheme
1313
{
1414
/// <summary>
1515
/// The name of the authentication scheme.

0 commit comments

Comments
 (0)