@@ -45,6 +45,7 @@ public class AccountController : IdentityServerController
4545 private readonly IAuthenticationSchemeProvider schemeProvider ;
4646 private readonly LearningHubAuthConfig authConfig ;
4747 private readonly WebSettings webSettings ;
48+ private readonly ILogger logger ;
4849
4950 /// <summary>
5051 /// Initializes a new instance of the <see cref="AccountController"/> class.
@@ -55,6 +56,7 @@ public class AccountController : IdentityServerController
5556 /// <param name="events">events parameter.</param>
5657 /// <param name="userService">userService parameter.</param>
5758 /// <param name="webSettings">webSettings parameter.</param>
59+ /// <param name="logger">ILogger instance.</param>
5860 /// <param name="authConfig">Auth service config.</param>
5961 /// <param name="cacheService">Cacje service config.</param>
6062 public AccountController (
@@ -64,6 +66,7 @@ public AccountController(
6466 IEventService events ,
6567 IUserService userService ,
6668 WebSettings webSettings ,
69+ ILogger < AccountController > logger ,
6770 IOptions < LearningHubAuthConfig > authConfig ,
6871 ICacheService cacheService )
6972 : base ( userService , events , clientStore , webSettings , cacheService )
@@ -72,6 +75,7 @@ public AccountController(
7275 this . schemeProvider = schemeProvider ;
7376 this . authConfig = authConfig ? . Value ;
7477 this . webSettings = webSettings ;
78+ this . logger = logger ;
7579 }
7680
7781 /// <summary>
@@ -145,6 +149,12 @@ await this.interaction.GrantConsentAsync(
145149 }
146150 }
147151
152+ if ( context == null )
153+ {
154+ this . ModelState . AddModelError ( string . Empty , "Invalid request" ) ;
155+ goto showFormWithError ;
156+ }
157+
148158 if ( this . ModelState . IsValid )
149159 {
150160 // validate username/password
@@ -208,6 +218,8 @@ await this.UserService.AddLogonToUserHistory(
208218 this . ModelState . AddModelError ( string . Empty , loginResult . ErrorMessage ) ;
209219 }
210220
221+ showFormWithError :
222+
211223 // something went wrong, show form with error
212224 var vm = await this . BuildLoginViewModelAsync ( model ) ;
213225 if ( ( vm . ClientId == "learninghubwebclient" ) || ( vm . ClientId == "learninghubadmin" ) )
@@ -328,7 +340,7 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
328340 {
329341 var context = await this . interaction . GetAuthorizationContextAsync ( returnUrl ) ;
330342 LoginClientTemplate loginClientTemplate = null ;
331- if ( context ? . Client . ClientId != null && this . authConfig . IdsClients . ContainsKey ( context . Client . ClientId ) )
343+ if ( context ? . Client ? . ClientId != null && this . authConfig . IdsClients . ContainsKey ( context . Client . ClientId ) )
332344 {
333345 loginClientTemplate = this . authConfig . IdsClients [ context . Client . ClientId ] ;
334346
@@ -375,7 +387,7 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
375387
376388 var allowLocal = true ;
377389
378- if ( context ? . Client . ClientId != null )
390+ if ( context ? . Client ? . ClientId != null )
379391 {
380392 var client = await this . ClientStore . FindEnabledClientByIdAsync ( context . Client . ClientId ) ;
381393 if ( client != null )
@@ -388,6 +400,12 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
388400 }
389401 }
390402
403+ if ( context == null || loginClientTemplate == null )
404+ {
405+ string message = context == null ? "context" : loginClientTemplate == null ? "clientTemplate" : string . Empty ;
406+ this . logger . LogWarning ( $ "return url has no { message } : { returnUrl } ") ;
407+ }
408+
391409 return new LoginViewModel
392410 {
393411 AllowRememberLogin = loginClientTemplate ? . AllowRememberLogin ?? AccountOptions . AllowRememberLogin ,
@@ -396,7 +414,7 @@ private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
396414 Username = context ? . LoginHint ,
397415 ExternalProviders = providers . ToArray ( ) ,
398416 LoginClientTemplate = loginClientTemplate ?? new LoginClientTemplate ( ) ,
399- ClientId = context . Client . ClientId ,
417+ ClientId = context ? . Client ? . ClientId ,
400418 } ;
401419 }
402420
0 commit comments