1
- using _2_1_Call_MSGraph . Models ;
1
+ using CallMSGraph . Models ;
2
2
using Microsoft . AspNetCore . Authorization ;
3
3
using Microsoft . AspNetCore . Mvc ;
4
+ using Microsoft . Extensions . Configuration ;
4
5
using Microsoft . Extensions . Logging ;
5
6
using Microsoft . Graph ;
6
7
using Microsoft . Identity . Web ;
9
10
using System . IO ;
10
11
using System . Threading . Tasks ;
11
12
12
- namespace _2_1_Call_MSGraph . Controllers
13
+ namespace CallMSGraph . Controllers
13
14
{
14
15
[ Authorize ]
15
16
public class HomeController : Controller
@@ -20,13 +21,18 @@ public class HomeController : Controller
20
21
21
22
private readonly MicrosoftIdentityConsentAndConditionalAccessHandler _consentHandler ;
22
23
24
+ private string [ ] _graphScopes = new [ ] { "user.read" } ;
25
+
23
26
public HomeController ( ILogger < HomeController > logger ,
24
- GraphServiceClient graphServiceClient ,
25
- MicrosoftIdentityConsentAndConditionalAccessHandler consentHandler )
27
+ IConfiguration configuration ,
28
+ GraphServiceClient graphServiceClient ,
29
+ MicrosoftIdentityConsentAndConditionalAccessHandler consentHandler )
26
30
{
27
31
_logger = logger ;
28
32
_graphServiceClient = graphServiceClient ;
29
33
this . _consentHandler = consentHandler ;
34
+
35
+ _graphScopes = configuration . GetValue < string > ( "DownstreamApi:Scopes" ) ? . Split ( ' ' ) ;
30
36
}
31
37
32
38
[ AuthorizeForScopes ( ScopeKeySection = "DownstreamApi:Scopes" ) ]
@@ -40,52 +46,30 @@ public IActionResult Index()
40
46
[ AuthorizeForScopes ( ScopeKeySection = "DownstreamApi:Scopes" ) ]
41
47
public async Task < IActionResult > Profile ( )
42
48
{
43
- Microsoft . Graph . User currentUser = null ;
49
+ User currentUser = null ;
44
50
45
51
try
46
52
{
47
53
currentUser = await _graphServiceClient . Me . Request ( ) . GetAsync ( ) ;
48
54
}
49
- catch ( System . Exception ex )
55
+ catch ( System . Exception ex ) // Catch CAE exception from Graph SDK
50
56
{
51
- if ( ex is WebApiMsalUiRequiredException || ( ex is ServiceException && ex . Message . Trim ( ) . Contains ( "Continuous access evaluation resulted in claims challenge" ) ) )
57
+ if ( ex is ServiceException && ex . Message . Trim ( ) . Contains ( "Continuous access evaluation resulted in claims challenge" ) )
52
58
{
53
- if ( ex is WebApiMsalUiRequiredException )
59
+ try
54
60
{
55
- try
56
- {
57
- WebApiMsalUiRequiredException hex = ex as WebApiMsalUiRequiredException ;
58
- Console . WriteLine ( $ "{ hex } ") ;
59
-
60
- var claimChallenge = AuthenticationHeaderHelper . ExtractHeaderValues ( hex ) ;
61
- _consentHandler . ChallengeUser ( new string [ ] { "user.read" } , claimChallenge ) ;
62
-
63
- return new EmptyResult ( ) ;
64
- }
65
- catch ( Exception ex2 )
66
- {
67
- _consentHandler . HandleException ( ex2 ) ;
68
- }
61
+ ServiceException svcex = ex as ServiceException ;
62
+ Console . WriteLine ( $ "{ svcex } ") ;
63
+ var claimChallenge = AuthenticationHeaderHelper . ExtractClaimChallengeFromHttpHeader ( svcex . ResponseHeaders ) ;
64
+ _consentHandler . ChallengeUser ( _graphScopes , claimChallenge ) ;
65
+ return new EmptyResult ( ) ;
69
66
}
70
-
71
- if ( ex is ServiceException )
67
+ catch ( Exception ex2 )
72
68
{
73
- try
74
- {
75
- ServiceException svcex = ex as ServiceException ;
76
- Console . WriteLine ( $ "{ svcex } ") ;
77
- var claimChallenge = AuthenticationHeaderHelper . ExtractHeaderValues ( svcex . ResponseHeaders ) ;
78
- _consentHandler . ChallengeUser ( new string [ ] { "user.read" } , claimChallenge ) ;
79
- return new EmptyResult ( ) ;
80
- }
81
- catch ( Exception ex2 )
82
- {
83
- _consentHandler . HandleException ( ex2 ) ;
84
- }
69
+ _consentHandler . HandleException ( ex2 ) ;
85
70
}
86
71
}
87
72
88
-
89
73
try
90
74
{
91
75
// Get user photo
@@ -100,7 +84,6 @@ public async Task<IActionResult> Profile()
100
84
Console . WriteLine ( $ "{ pex } ") ;
101
85
ViewData [ "Photo" ] = null ;
102
86
}
103
-
104
87
}
105
88
ViewData [ "Me" ] = currentUser ;
106
89
return View ( ) ;
0 commit comments