You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most of the time, you will want to use bearer authentication so that you can use a JWT (Json Web Token) obtained from an OIDC server. This is so prevalent that we provide an easy mechanism to add this to your application via a `GenericAuthenticationProvider`. The authentication provider only requests tokens from your token retrieval method when required (when the provided token is close to expiring or has expired).
7
+
8
+
The `GenericAuthenticationProvider` and associated classes are in the `CommunityToolkit.Datasync.Client.Authentication` namespace.
9
+
10
+
## Set up authentication and authorization on the datasync service
11
+
12
+
You must set up authentication and authorization on the datasync service first. The authentication and authorization is regular ASP.NET Core
13
+
identity, so [follow the instructions](https://learn.microsoft.com/aspnet/core/security/) for your particular provider.
14
+
15
+
## Create a method to retrieve the token
16
+
17
+
You need to implement a method to retrieve the token. Normally, this uses the library that is provided for the purpose. For example:
18
+
19
+
* Microsoft logins use [Microsoft.Identity.Client](https://www.nuget.org/packages/Microsoft.Identity.Client).
20
+
* Other logins on MAUI may use [WebAuthenticator](https://learn.microsoft.com/dotnet/maui/platform-integration/communication/authentication)
21
+
22
+
Whatever mechanism you use, this must be set up first. If your application is unable to get a token, the authentication middleware cannot pass it onto the server.
23
+
24
+
## Add the GenericAuthenticationProvider to your client
25
+
26
+
The `GenericAuthenticationProvider` takes a function that retrieves the token. For example:
### Build HttpClientOptions with the authentication provider
51
+
52
+
The authentication provider is a `DelegatingHandler`, so it belongs in the `HttpPipeline`:
53
+
54
+
```csharp
55
+
HttpClientOptionsoptions=new()
56
+
{
57
+
HttpPipeline= [ authProvider ],
58
+
Endpont="https://myservice.azurewebsites.net"
59
+
};
60
+
```
61
+
62
+
You can then use this options structure when constructing a client (either in the `OnDatasyncInitialization()` method or when constructing the `DatasyncServiceClient`).
63
+
64
+
> [!TIP]
65
+
> It's normal to inject the authentication provider as a singleton in an MVVM scenario with dependency injection.
66
+
67
+
## Forcing a login request
68
+
69
+
Sometimes, you want to force a login request; for example, in response to a button click. You can call `LoginAsync()` on the authentication provider to trigger a login sequence. The token will then be used until it expires.
70
+
71
+
## Refresh token
72
+
73
+
Most providers allow you to request a "refresh token" that can be used to silently request an access token for use in accessing the datasync service. You can store and retrieve refresh tokens from local storage in your token retrieval method. The `GenericAuthenticationProvider` does not natively handle refresh tokens for you.
74
+
75
+
## Other options
76
+
77
+
You can specify which header is used for authorization. For example, Azure App Service Authentication and Authorization service uses the `X-ZUMO-AUTH` header to transmit the token. This is easily set up:
This gives you significant flexibility to build the authentication mechanism appropriate for your application.
90
+
91
+
By default, a new token is requested if the old token is expired or within 2 minutes of expiry. You can adjust the amount of buffer time using the `RefreshBufferTimeSpan` property:
Copy file name to clipboardExpand all lines: docs/content/in-depth/client/oneline-operations.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,9 @@ public IHttpClientFactory GetClientFactory()
55
55
56
56
The first element in the list becomes the root handler, then each successive handler is chained to the `InnerHandler` of the previous handler.
57
57
58
+
> [!TIP]
59
+
> You can easily set up basic and bearer authentication using the `GenericAuthenticationProvider`. See the [authentication guide](./auth.md) for more details.
60
+
58
61
## Create a Datasync Service Client
59
62
60
63
Now that you have something to generate `HttpClient` objects, you can use it to create a `DatasyncServiceClient` for a specific service:
0 commit comments