@@ -9,7 +9,7 @@ ms.service: active-directory
9
9
ms.subservice : develop
10
10
ms.topic : tutorial
11
11
ms.workload : identity
12
- ms.date : 12/13/2019
12
+ ms.date : 03/03/2023
13
13
ms.author : henrymbugua
14
14
ms.reviewer : jmprieur
15
15
ms.custom : " devx-track-csharp, aaddev, identityplatformtop40"
@@ -108,6 +108,7 @@ This section shows how to use the Microsoft Authentication Library to get a toke
108
108
``` csharp
109
109
using Microsoft .Identity .Client ;
110
110
using Microsoft .Graph ;
111
+ using Microsoft .Graph .Models ;
111
112
using System .Diagnostics ;
112
113
using System .Threading .Tasks ;
113
114
using System .Net .Http .Headers ;
@@ -152,7 +153,7 @@ This section shows how to use the Microsoft Authentication Library to get a toke
152
153
GraphServiceClient graphClient = await SignInAndInitializeGraphServiceClient (scopes );
153
154
154
155
// Call the /me endpoint of Graph
155
- User graphUser = await graphClient .Me .Request (). GetAsync ();
156
+ User graphUser = await graphClient .Me .GetAsync ();
156
157
157
158
// Go back to the UI thread to make changes to the UI
158
159
await Dispatcher .RunAsync (Windows .UI .Core .CoreDispatcherPriority .Normal , () =>
@@ -236,6 +237,37 @@ Eventually, the `AcquireTokenSilent` method fails. Reasons for failure include a
236
237
237
238
### Instantiate the Microsoft Graph Service Client by obtaining the token from the SignInUserAndGetTokenUsingMSAL method
238
239
240
+ In the project , create a new file named * TokenProvider .cs * with the following code :
241
+
242
+ ```csharp
243
+ using Microsoft .Kiota .Abstractions .Authentication ;
244
+ using System ;
245
+ using System .Collections .Generic ;
246
+ using System .Threading ;
247
+ using System .Threading .Tasks ;
248
+
249
+ namespace UWP_app_MSGraph {
250
+ public class TokenProvider : IAccessTokenProvider {
251
+ private Func <string [], Task <string >> getTokenDelegate ;
252
+ private string [] scopes ;
253
+
254
+ public TokenProvider (Func <string [], Task <string >> getTokenDelegate , string [] scopes ) {
255
+ this .getTokenDelegate = getTokenDelegate ;
256
+ this .scopes = scopes ;
257
+ }
258
+
259
+ public Task <string > GetAuthorizationTokenAsync (Uri uri , Dictionary <string , object > additionalAuthenticationContext = default ,
260
+ CancellationToken cancellationToken = default ) {
261
+ return getTokenDelegate (scopes );
262
+ }
263
+
264
+ public AllowedHostsValidator AllowedHostsValidator { get ; }
265
+ }
266
+ }
267
+ ```
268
+
269
+ This class defines a custom access token provider that executes the specified delegate method to get and and return an access token.
270
+
239
271
Add the following new method to * MainPage.xaml.cs* :
240
272
241
273
``` csharp
@@ -245,16 +277,16 @@ Add the following new method to *MainPage.xaml.cs*:
245
277
/// <returns >GraphServiceClient</returns >
246
278
private async static Task < GraphServiceClient > SignInAndInitializeGraphServiceClient (string [] scopes )
247
279
{
248
- GraphServiceClient graphClient = new GraphServiceClient (MSGraphURL ,
249
- new DelegateAuthenticationProvider (async (requestMessage ) =>
250
- {
251
- requestMessage .Headers .Authorization = new AuthenticationHeaderValue (" bearer" , await SignInUserAndGetTokenUsingMSAL (scopes ));
252
- }));
280
+ var tokenProvider = new TokenProvider (SignInUserAndGetTokenUsingMSAL , scopes );
281
+ var authProvider = new BaseBearerTokenAuthenticationProvider (tokenProvider );
282
+ var graphClient = new GraphServiceClient (authProvider , MSGraphURL );
253
283
254
284
return await Task .FromResult (graphClient );
255
285
}
256
286
```
257
287
288
+ In this method, you're using the custom access token provider ` TokenProvider ` to connect the ` SignInUserAndGetTokenUsingMSAL ` method to the Microsoft Graph .NET SDK and create an authenticated client.
289
+
258
290
#### More information on making a REST call against a protected API
259
291
260
292
In this sample application, the ` GetGraphServiceClient ` method instantiates ` GraphServiceClient ` by using an access token. Then, ` GraphServiceClient ` is used to get the user's profile information from the ** me** endpoint.
0 commit comments