Skip to content

Commit 439c956

Browse files
Update OIDC client documentation with examples and clarifications
Added expanded examples and code snippets for `OidcClient` configuration, logging, automatic mode, and manual mode. Clarified the use of `IBrowser` in authentication flows and highlighted differences between modes, providing links to relevant references and structured explanations.
1 parent fec1888 commit 439c956

3 files changed

Lines changed: 153 additions & 15 deletions

File tree

src/content/docs/identitymodel-oidcclient/automatic.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,81 @@ redirect_from:
88
- /foss/identitymodel.oidcclient/automatic/
99
---
1010

11-
In automatic mode, you can encapsulate all browser interactions by
12-
implementing the
13-
[IBrowser](https://github.com/DuendeArchive/IdentityModel.OidcClient/blob/main/src/OidcClient/Browser/IBrowser.cs)
14-
interface:
11+
OpenID Connect (OIDC) is a simple identity layer on top of the OAuth 2.0
12+
protocol. It allows clients to verify the identity of the end-user based on
13+
the authentication performed by an authorization server, as well as obtain
14+
basic profile information.
15+
16+
An essential part of the OIDC flow is the use of a browser to interact with the
17+
end-user and to obtain permissions to access protected resources.
18+
19+
In the OidcClient library, you can encapsulate the browser interaction by implementing the
20+
[IBrowser](https://github.com/DuendeSoftware/foss/blob/main/identity-model-oidc-client/src/IdentityModel.OidcClient/Browser/IBrowser.cs)
21+
interface. Using `IBrowser` helps create a reusable component for all OIDC interaction.
22+
23+
```csharp
24+
// Copyright (c) Duende Software. All rights reserved.
25+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
26+
27+
namespace Duende.IdentityModel.OidcClient.Browser;
28+
29+
/// <summary>
30+
/// Models a browser
31+
/// </summary>
32+
public interface IBrowser
33+
{
34+
/// <summary>
35+
/// Invokes the browser.
36+
/// </summary>
37+
/// <param name="options">The options.</param>
38+
/// <param name="cancellationToken">A token that can be used to cancel the request</param>
39+
/// <returns></returns>
40+
Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default);
41+
}
42+
```
43+
44+
The `BrowserResult` represents the result of the browser interaction, including any OIDC payloads that
45+
are returned from the authentication server.
46+
47+
```csharp
48+
// Copyright (c) Duende Software. All rights reserved.
49+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
50+
51+
namespace Duende.IdentityModel.OidcClient.Browser;
52+
53+
/// <summary>
54+
/// The result from a browser login.
55+
/// </summary>
56+
/// <seealso cref="Result" />
57+
public class BrowserResult : Result
58+
{
59+
/// <summary>
60+
/// Gets or sets the type of the result.
61+
/// </summary>
62+
/// <value>
63+
/// The type of the result.
64+
/// </value>
65+
public BrowserResultType ResultType { get; set; }
66+
67+
/// <summary>
68+
/// Gets or sets the response.
69+
/// </summary>
70+
/// <value>
71+
/// The response.
72+
/// </value>
73+
public string Response { get; set; }
74+
}
75+
```
76+
77+
:::note
78+
The `IBrowser` implementation must be specific to the platform and environment and must be provided by the
79+
host application. For example, a Windows-specific implementation will not work within a macOS, iOS, Android, or Linux environment.
80+
:::
81+
82+
For a simple example, the following code shows how to use the
83+
[SystemBrowser](https://github.com/DuendeSoftware/foss/blob/main/identity-model-oidc-client/clients/ConsoleClientWithBrowser/SystemBrowser.cs)
84+
to invoke a browser on the host desktop platform. The `SystemBrowser` is a naive implementation that uses the
85+
[System.Diagnostics.Process](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process) class to invoke the browser.
1586

1687
```cs
1788
var options = new OidcClientOptions
@@ -26,9 +97,13 @@ var options = new OidcClientOptions
2697
var client = new OidcClient(options);
2798
```
2899

29-
Once that is done, authentication and token requests become one line of
30-
code:
100+
Once the `IBrowser` is configured, the `LoginAsync` method can be invoked to start the authentication flow.
31101

32102
```cs
33103
var result = await client.LoginAsync();
34104
```
105+
106+
Setting the `Browser` property reduces the need to process browser respones and to handle the `BrowserResult` directly. When using this automatic mode, the `LoginAsync` method will return a
107+
[`LoginResult`](https://github.com/DuendeSoftware/foss/blob/19370c6d4820a684d41d1d40b8192ee8b873b8f0/identity-model-oidc-client/src/IdentityModel.OidcClient/LoginResult.cs) which will contain a `ClaimsPrincipal` with the user's claims along with the `IdentityToken` and `AccessToken`.
108+
109+

src/content/docs/identitymodel-oidcclient/logging.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,38 @@ redirect_from:
88
- /foss/identitymodel.oidcclient/logging/
99
---
1010

11-
OidcClient logs errors, warnings, and diagnostic information using
12-
*Microsoft.Extensions.Logging.ILogger*, the standard .NET logging library. You can use any
13-
logging provider to store your logs however you like. For example, you could configure
11+
`OidcClient` logs errors, warnings, and diagnostic information using
12+
`Microsoft.Extensions.Logging.ILogger`, the standard .NET logging library.
13+
14+
```cs
15+
using Duende.IdentityModel;
16+
using Duende.IdentityModel.OidcClient;
17+
18+
var builder = Host.CreateApplicationBuilder(args);
19+
20+
builder.Services.AddSingleton(svc =>
21+
{
22+
var loggerFactory = svc.GetRequiredService<ILoggerFactory>();
23+
var options = new OidcClientOptions
24+
{
25+
Authority = "https://demo.duendesoftware.com",
26+
ClientId = "interactive.public",
27+
Scope = "openid profile email offline_access",
28+
RedirectUri = "app://localhost/",
29+
PostLogoutRedirectUri = "app://localhost/",
30+
LoggerFactory = loggerFactory
31+
};
32+
return new OidcClient(options);
33+
});
34+
35+
var app = builder.Build();
36+
var client = app.Services.GetService<OidcClient>();
37+
```
38+
39+
You can use any
40+
logging provider to store your logs however you like by setting the `LoggerFactory` property on `OidcClientOptions`.
41+
42+
For example, you could configure
1443
[Serilog](https://github.com/serilog/serilog-extensions-hosting) like this:
1544

1645
```csharp
@@ -22,3 +51,27 @@ var serilog = new LoggerConfiguration()
2251

2352
options.LoggerFactory.AddSerilog(serilog);
2453
```
54+
55+
## Log Levels
56+
57+
The `OidcClient` logs at the following levels:
58+
59+
- `Trace`
60+
- `Debug`
61+
- `Information`
62+
- `Error`
63+
64+
You can set the log level in your `appSettings.json` by modifying the following snippet.
65+
66+
```json
67+
{
68+
"Logging": {
69+
"LogLevel": {
70+
"Default": "Information",
71+
"Microsoft": "Warning",
72+
"Microsoft.Hosting.Lifetime": "Information",
73+
"Duende.IdentityModel.OidcClient": "Error"
74+
}
75+
}
76+
}
77+
```

src/content/docs/identitymodel-oidcclient/manual.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ title: OIDC Client Manual Mode
33
description: "Guide for implementing manual mode in OidcClient to handle browser interactions and token processing"
44
sidebar:
55
order: 2
6+
label: Manual Mode
67
redirect_from:
78
- /foss/identitymodel.oidcclient/manual/
89
---
910

10-
In manual mode, OidcClient helps you with creating the necessary start
11-
URL and state parameters, but you need to coordinate with whatever
12-
browser you want to use, e.g.:
11+
OpenID Connect is a protocol that allows you to authenticate users
12+
using a browser and involves browser-based interactions. When using this
13+
library you can choose between two modes: [automatic](./automatic.md) and manual.
14+
15+
We recommend using automatic mode when possible, but sometimes you need
16+
to use manual mode when you want to handle browser interactions yourself.
17+
18+
With manual mode, `OidcClient` is still useful, as it helps
19+
with creating the necessary start URL and state parameters needed to complete an OIDC flow.
20+
You'll need to handle all browser interactions yourself with custom code. This is beneficial
21+
for scenarios where you want to customize the browser experience or when you want to
22+
integrate with other platform-specific browser libraries.
1323

1424
```csharp
1525
var options = new OidcClientOptions
@@ -26,13 +36,13 @@ var client = new OidcClient(options);
2636
var state = await client.PrepareLoginAsync();
2737
```
2838

29-
When the browser work is done, OidcClient can take over to process the
39+
When the browser work is done, `OidcClient` can take over to process the
3040
response, get the access/refresh tokens, contact userinfo endpoint
3141
etc.:
3242

3343
```csharp
3444
var result = await client.ProcessResponseAsync(data, state);
3545
```
3646

37-
The result will contain the tokens and the claims of the user.
38-
47+
When using this manual mode, and processing the response, the `ProcessResponseAsync` method will return a
48+
[`LoginResult`](https://github.com/DuendeSoftware/foss/blob/19370c6d4820a684d41d1d40b8192ee8b873b8f0/identity-model-oidc-client/src/IdentityModel.OidcClient/LoginResult.cs) which will contain a `ClaimsPrincipal` with the user's claims along with the `IdentityToken` and `AccessToken`.

0 commit comments

Comments
 (0)