@@ -169,55 +169,54 @@ In the `Controllers\HomeController.cs`file:
169169
1701701 . Add a constructor to HomeController , making the ITokenAcquisition service available (used by the ASP .NET dependency injection mechanism )
171171
172- ```CSharp
173- readonly ITokenAcquisition tokenAcquisition ;
174- readonly WebOptions webOptions ;
172+ ```CSharp
173+ readonly ITokenAcquisition tokenAcquisition ;
174+ readonly WebOptions webOptions ;
175175
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+ ```
182182
1831831 . 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 .
184184
185185```CSharp
186- [AuthorizeForScopes (Scopes = new [] {Constants .ScopeUserRead })]
186+ [AuthorizeForScopes (Scopes = new [] { Constants .ScopeUserRead })]
187187public async Task < IActionResult > Profile ()
188188{
189- // Initialize the GraphServiceClient.
190- Graph :: GraphServiceClient graphClient = GetGraphServiceClient (new [] { Constants .ScopeUserRead });
189+ // Initialize the GraphServiceClient.
190+ Graph :: GraphServiceClient graphClient = GetGraphServiceClient (new [] { Constants .ScopeUserRead });
191191
192- var me = await graphClient .Me .Request ().GetAsync ();
193- ViewData [" Me" ] = me ;
192+ var me = await graphClient .Me .Request ().GetAsync ();
193+ ViewData [" Me" ] = me ;
194194
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 ())
205199 {
206- ViewData [" Photo" ] = null ;
200+ byte [] photoByte = ((MemoryStream )photoStream ).ToArray ();
201+ ViewData [" Photo" ] = Convert .ToBase64String (photoByte );
207202 }
203+ }
204+ catch (System .Exception )
205+ {
206+ ViewData [" Photo" ] = null ;
207+ }
208208
209- return View ();
209+ return View ();
210210}
211211
212212private Graph :: GraphServiceClient GetGraphServiceClient (string [] scopes )
213213{
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 );
219219}
220-
221220```
222221
223222### 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
234233< h3 > @ViewData [" Message" ]< / h3 >
235234
236235< 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 >
244243 @{
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+ }
254253 }
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+ }
281280< / table >
282281```
283282
0 commit comments