@@ -169,55 +169,54 @@ In the `Controllers\HomeController.cs`file:
169
169
170
170
1 . Add a constructor to HomeController , making the ITokenAcquisition service available (used by the ASP .NET dependency injection mechanism )
171
171
172
- ```CSharp
173
- readonly ITokenAcquisition tokenAcquisition ;
174
- readonly WebOptions webOptions ;
172
+ ```CSharp
173
+ readonly ITokenAcquisition tokenAcquisition ;
174
+ readonly WebOptions webOptions ;
175
175
176
- public HomeController (ITokenAcquisition tokenAcquisition , IOptions < WebOptions > webOptionValue )
177
- {
178
- this .tokenAcquisition = tokenAcquisition ;
179
- this .webOptions = webOptionValue .Value ;
180
- }
181
- ```
176
+ public HomeController (ITokenAcquisition tokenAcquisition , IOptions < WebOptions > webOptionValue )
177
+ {
178
+ this .tokenAcquisition = tokenAcquisition ;
179
+ this .webOptions = webOptionValue .Value ;
180
+ }
181
+ ```
182
182
183
183
1 . Add a `Profile ()` action so that it calls the Microsoft Graph * me * endpoint . In case a token cannot be acquired , a challenge is attempted to re -sign -in the user , and have them consent to the requested scopes . This is expressed declaratively by the `AuthorizeForScopes `attribute . This attribute is part of the `Microsoft .Identity .Web ` project and automatically manages incremental consent .
184
184
185
185
```CSharp
186
- [AuthorizeForScopes (Scopes = new [] {Constants .ScopeUserRead })]
186
+ [AuthorizeForScopes (Scopes = new [] { Constants .ScopeUserRead })]
187
187
public async Task < IActionResult > Profile ()
188
188
{
189
- // Initialize the GraphServiceClient.
190
- Graph :: GraphServiceClient graphClient = GetGraphServiceClient (new [] { Constants .ScopeUserRead });
189
+ // Initialize the GraphServiceClient.
190
+ Graph :: GraphServiceClient graphClient = GetGraphServiceClient (new [] { Constants .ScopeUserRead });
191
191
192
- var me = await graphClient .Me .Request ().GetAsync ();
193
- ViewData [" Me" ] = me ;
192
+ var me = await graphClient .Me .Request ().GetAsync ();
193
+ ViewData [" Me" ] = me ;
194
194
195
- try
196
- {
197
- // Get user photo
198
- using (var photoStream = await graphClient .Me .Photo .Content .Request ().GetAsync ())
199
- {
200
- byte [] photoByte = ((MemoryStream )photoStream ).ToArray ();
201
- ViewData [" Photo" ] = Convert .ToBase64String (photoByte );
202
- }
203
- }
204
- catch (System .Exception )
195
+ try
196
+ {
197
+ // Get user photo
198
+ using (var photoStream = await graphClient .Me .Photo .Content .Request ().GetAsync ())
205
199
{
206
- ViewData [" Photo" ] = null ;
200
+ byte [] photoByte = ((MemoryStream )photoStream ).ToArray ();
201
+ ViewData [" Photo" ] = Convert .ToBase64String (photoByte );
207
202
}
203
+ }
204
+ catch (System .Exception )
205
+ {
206
+ ViewData [" Photo" ] = null ;
207
+ }
208
208
209
- return View ();
209
+ return View ();
210
210
}
211
211
212
212
private Graph :: GraphServiceClient GetGraphServiceClient (string [] scopes )
213
213
{
214
- return GraphServiceClientFactory .GetAuthenticatedGraphClient (async () =>
215
- {
216
- string result = await tokenAcquisition .GetAccessTokenOnBehalfOfUserAsync (scopes );
217
- return result ;
218
- }, webOptions .GraphApiUrl );
214
+ return GraphServiceClientFactory .GetAuthenticatedGraphClient (async () =>
215
+ {
216
+ string result = await tokenAcquisition .GetAccessTokenOnBehalfOfUserAsync (scopes );
217
+ return result ;
218
+ }, webOptions .GraphApiUrl );
219
219
}
220
-
221
220
```
222
221
223
222
### Add a Profile view to display the *me* object
@@ -234,50 +233,50 @@ HTML table displaying the properties of the *me* object as returned by Microsoft
234
233
< h3 > @ViewData [" Message" ]< / h3 >
235
234
236
235
< table class = " table table-striped table-condensed" style = " font-family: monospace" >
237
- < tr >
238
- < th > Property < / th >
239
- < th > Value < / th >
240
- < / tr >
241
- < tr >
242
- < td > photo < / td >
243
- < td >
236
+ < tr >
237
+ < th > Property < / th >
238
+ < th > Value < / th >
239
+ < / tr >
240
+ < tr >
241
+ < td > photo < / td >
242
+ < td >
244
243
@{
245
- if (ViewData [" photo" ] != null )
246
- {
247
- < img style = " margin: 5px 0; width: 150px" src = " data:image/jpeg;base64, @ViewData[" photo " ]" / >
248
- }
249
- else
250
- {
251
- < h3 > NO PHOTO < / h3 >
252
- < p > Check user profile in Azure Active Directory to add a photo .< / p >
253
- }
244
+ if (ViewData [" photo" ] != null )
245
+ {
246
+ < img style = " margin: 5px 0; width: 150px" src = " data:image/jpeg;base64, @ViewData[" photo " ]" / >
247
+ }
248
+ else
249
+ {
250
+ < h3 > NO PHOTO < / h3 >
251
+ < p > Check user profile in Azure Active Directory to add a photo .< / p >
252
+ }
254
253
}
255
- < / td >
256
- < / tr >
257
- @{
258
- var me = ViewData [" me" ] as Microsoft .Graph .User ;
259
- var properties = me .GetType ().GetProperties ();
260
- foreach (var child in properties )
261
- {
262
- object value = child .GetValue (me );
263
- string stringRepresentation ;
264
- if (! (value is string ) && value is IEnumerable <string >)
265
- {
266
- stringRepresentation = " ["
267
- + string .Join (" , " , (value as IEnumerable <string >).OfType <object >().Select (c => c .ToString ()))
268
- + " ]" ;
269
- }
270
- else
271
- {
272
- stringRepresentation = value ? .ToString ();
273
- }
274
-
275
- < tr >
276
- < td > @child .Name < / td >
277
- < td > @stringRepresentation < / td >
278
- < / tr >
279
- }
280
- }
254
+ < / td >
255
+ < / tr >
256
+ @{
257
+ var me = ViewData [" me" ] as Microsoft .Graph .User ;
258
+ var properties = me .GetType ().GetProperties ();
259
+ foreach (var child in properties )
260
+ {
261
+ object value = child .GetValue (me );
262
+ string stringRepresentation ;
263
+ if (! (value is string ) && value is IEnumerable <string >)
264
+ {
265
+ stringRepresentation = " ["
266
+ + string .Join (" , " , (value as IEnumerable <string >).OfType <object >().Select (c => c .ToString ()))
267
+ + " ]" ;
268
+ }
269
+ else
270
+ {
271
+ stringRepresentation = value ? .ToString ();
272
+ }
273
+
274
+ < tr >
275
+ < td > @child .Name < / td >
276
+ < td > @stringRepresentation < / td >
277
+ < / tr >
278
+ }
279
+ }
281
280
< / table >
282
281
```
283
282
0 commit comments