@@ -91,6 +91,7 @@ protected override async Task ApplyResponseGrantAsync()
9191 {
9292 ProtocolMessage = openIdConnectMessage
9393 } ;
94+
9495 await Options . Notifications . RedirectToIdentityProvider ( notification ) ;
9596
9697 if ( ! notification . HandledResponse )
@@ -100,6 +101,7 @@ protected override async Task ApplyResponseGrantAsync()
100101 {
101102 _logger . WriteWarning ( "The logout redirect URI is malformed: " + redirectUri ) ;
102103 }
104+
103105 Response . Redirect ( redirectUri ) ;
104106 }
105107 }
@@ -116,15 +118,30 @@ protected override void ApplyResponseChallenge()
116118 /// <returns></returns>
117119 protected override async Task ApplyResponseChallengeAsync ( )
118120 {
119- if ( ( Response . StatusCode != 401 ) || ( ChallengeContext == null ) )
121+ if ( Response . StatusCode != 401 )
122+ {
123+ return ;
124+ }
125+
126+ // Active middleware should redirect on 401 even if there wasn't an explicit challenge.
127+ if ( ChallengeContext == null && Options . AuthenticationMode == AuthenticationMode . Passive )
120128 {
121129 return ;
122130 }
123131
124132 // order for redirect_uri
125133 // 1. challenge.Properties.RedirectUri
126134 // 2. CurrentUri
127- AuthenticationProperties properties = new AuthenticationProperties ( ChallengeContext . Properties ) ;
135+ AuthenticationProperties properties ;
136+ if ( ChallengeContext == null )
137+ {
138+ properties = new AuthenticationProperties ( ) ;
139+ }
140+ else
141+ {
142+ properties = new AuthenticationProperties ( ChallengeContext . Properties ) ;
143+ }
144+
128145 if ( string . IsNullOrEmpty ( properties . RedirectUri ) )
129146 {
130147 properties . RedirectUri = CurrentUri ;
@@ -154,7 +171,6 @@ protected override async Task ApplyResponseChallengeAsync()
154171 State = OpenIdConnectAuthenticationDefaults . AuthenticationPropertiesKey + "=" + Uri . EscapeDataString ( Options . StateDataFormat . Protect ( properties ) )
155172 } ;
156173
157- // TODO - brentschmaltz, if INonceCache is set should we even consider if ProtocolValidator is set?
158174 if ( Options . ProtocolValidator . RequireNonce )
159175 {
160176 openIdConnectMessage . Nonce = Options . ProtocolValidator . GenerateNonce ( ) ;
@@ -179,7 +195,7 @@ protected override async Task ApplyResponseChallengeAsync()
179195 string redirectUri = notification . ProtocolMessage . CreateAuthenticationRequestUrl ( ) ;
180196 if ( ! Uri . IsWellFormedUriString ( redirectUri , UriKind . Absolute ) )
181197 {
182- _logger . WriteWarning ( "The authenticate redirect URI is malformed : " + redirectUri ) ;
198+ _logger . WriteWarning ( "Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute) returned 'false', redirectUri is : " + ( redirectUri ?? "null" ) ) ;
183199 }
184200
185201 Response . Redirect ( redirectUri ) ;
0 commit comments