@@ -29,30 +29,37 @@ public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceCli
29
29
30
30
public async Task OnGet ( )
31
31
{
32
- var user = await _graphServiceClient . Me . GetAsync ( r =>
33
- r . Options . WithScopes ( "user.read" )
34
- //.WithUser(User)
35
- ) ;
36
-
32
+ try
33
+ {
34
+ var user = await _graphServiceClient . Me . GetAsync ( r =>
35
+ r . Options . WithScopes ( "user.read" )
36
+ //.WithUser(User)
37
+ ) ;
38
+ ViewData [ "name" ] = user . DisplayName ;
39
+ }
40
+ catch ( Exception )
41
+ {
42
+ ViewData [ "name" ] = null ;
43
+ }
44
+
37
45
try
38
46
{
39
47
using ( var photoStream = await _graphServiceClient . Me . Photo . Content . GetAsync ( ) )
40
48
{
41
49
byte [ ] photoByte = ( ( MemoryStream ) photoStream ) . ToArray ( ) ;
42
50
ViewData [ "photo" ] = Convert . ToBase64String ( photoByte ) ;
43
51
}
44
- ViewData [ "name" ] = user . DisplayName ;
45
-
46
- var graphData = await _downstreamApi . CallApiForUserAsync (
47
- "GraphBeta"
48
- ) ;
49
- ViewData [ "json" ] = await graphData . Content . ReadAsStringAsync ( ) ;
50
52
}
51
53
catch ( Exception )
52
54
{
53
55
ViewData [ "photo" ] = null ;
54
56
}
55
57
58
+ var graphData = await _downstreamApi . CallApiForUserAsync (
59
+ "GraphBeta"
60
+ ) ;
61
+ ViewData [ "json" ] = await graphData . Content . ReadAsStringAsync ( ) ;
62
+
56
63
// Or - Call a downstream directly with the IDownstreamApi helper (uses the authorization header provider, encapsulates MSAL.NET)
57
64
// See https://aka.ms/ms-id-web/downstream-web-api
58
65
IDownstreamApi downstreamApi = HttpContext . RequestServices . GetService ( typeof ( IDownstreamApi ) ) as IDownstreamApi ;
@@ -70,7 +77,7 @@ public async Task OnGet()
70
77
new AuthorizationHeaderProviderOptions { BaseUrl = "https://graph.microsoft.com/v1.0/me" } ) ;
71
78
HttpClient client = new HttpClient ( ) ;
72
79
client . DefaultRequestHeaders . Add ( "Authorization" , authorizationHeader ) ;
73
- HttpResponseMessage response = await client . GetAsync ( "https://graph.microsoft.com/v1.0/users " ) ;
80
+ HttpResponseMessage response = await client . GetAsync ( "https://graph.microsoft.com/v1.0/me " ) ;
74
81
75
82
// Or - Get a token if an SDK needs it (uses MSAL.NET)
76
83
ITokenAcquirerFactory tokenAcquirerFactory = HttpContext . RequestServices . GetService ( typeof ( ITokenAcquirerFactory ) ) as ITokenAcquirerFactory ;
0 commit comments