@@ -46,6 +46,7 @@ public class AuthorizeForScopesAttribute : ExceptionFilterAttribute
46
46
public override void OnException ( ExceptionContext context )
47
47
{
48
48
MsalUiRequiredException msalUiRequiredException = context . Exception as MsalUiRequiredException ;
49
+
49
50
if ( msalUiRequiredException == null )
50
51
{
51
52
msalUiRequiredException = context . Exception ? . InnerException as MsalUiRequiredException ;
@@ -55,6 +56,9 @@ public override void OnException(ExceptionContext context)
55
56
{
56
57
if ( CanBeSolvedByReSignInOfUser ( msalUiRequiredException ) )
57
58
{
59
+ // Do not re-use the attribute param Scopes. For more info: https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/issues/273
60
+ string [ ] scopes = null ;
61
+
58
62
// the users cannot provide both scopes and ScopeKeySection at the same time
59
63
if ( ! string . IsNullOrWhiteSpace ( ScopeKeySection ) && Scopes != null && Scopes . Length > 0 )
60
64
{
@@ -72,10 +76,13 @@ public override void OnException(ExceptionContext context)
72
76
throw new InvalidOperationException ( $ "The { nameof ( ScopeKeySection ) } is provided but the IConfiguration instance is not present in the services collection") ;
73
77
}
74
78
75
- Scopes = new string [ ] { configuration . GetValue < string > ( ScopeKeySection ) } ;
79
+ scopes = new string [ ] { configuration . GetValue < string > ( ScopeKeySection ) } ;
76
80
}
77
81
78
- var properties = BuildAuthenticationPropertiesForIncrementalConsent ( Scopes , msalUiRequiredException , context . HttpContext ) ;
82
+ else
83
+ scopes = Scopes ;
84
+
85
+ var properties = BuildAuthenticationPropertiesForIncrementalConsent ( scopes , msalUiRequiredException , context . HttpContext ) ;
79
86
context . Result = new ChallengeResult ( properties ) ;
80
87
}
81
88
}
@@ -91,7 +98,7 @@ private bool CanBeSolvedByReSignInOfUser(MsalUiRequiredException ex)
91
98
// InMemoryCache, the cache could be empty if the server was restarted. This is why
92
99
// the null_user exception is thrown.
93
100
94
- return ex . ErrorCode . ContainsAny ( new [ ] { MsalError . UserNullError , MsalError . InvalidGrantError } ) ;
101
+ return ex . ErrorCode . ContainsAny ( new [ ] { MsalError . UserNullError , MsalError . InvalidGrantError } ) ;
95
102
}
96
103
97
104
/// <summary>
@@ -102,16 +109,16 @@ private bool CanBeSolvedByReSignInOfUser(MsalUiRequiredException ex)
102
109
/// <param name="context">current http context in the pipeline</param>
103
110
/// <returns>AuthenticationProperties</returns>
104
111
private AuthenticationProperties BuildAuthenticationPropertiesForIncrementalConsent (
105
- string [ ] scopes ,
106
- MsalUiRequiredException ex ,
112
+ string [ ] scopes ,
113
+ MsalUiRequiredException ex ,
107
114
HttpContext context )
108
115
{
109
116
var properties = new AuthenticationProperties ( ) ;
110
117
111
118
// Set the scopes, including the scopes that ADAL.NET / MSAL.NET need for the token cache
112
119
string [ ] additionalBuiltInScopes =
113
- { OidcConstants . ScopeOpenId ,
114
- OidcConstants . ScopeOfflineAccess ,
120
+ { OidcConstants . ScopeOpenId ,
121
+ OidcConstants . ScopeOfflineAccess ,
115
122
OidcConstants . ScopeProfile } ;
116
123
properties . SetParameter < ICollection < string > > ( OpenIdConnectParameterNames . Scope ,
117
124
scopes . Union ( additionalBuiltInScopes ) . ToList ( ) ) ;
0 commit comments