public async Task <IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { var context = await this.interactionService.GetAuthorizationContextAsync(model.ReturnUrl); if (context == null) { return(BadRequest()); } if (model.Ldap) { var ldapUser = ldap.ValidateUser(model.UserName, model.Password); if (ldapUser != null) { var result = await signInManager.ExternalLoginSignInAsync(nameof(Provider.LDAP), ldapUser.Id.ToString(), isPersistent : model.RememberMe); if (!result.Succeeded) { var user = await this.userManager.FindByNameAsync(model.UserName); if (user == null) { user = new AppUser(model.UserName) { Email = ldapUser.Email, EmailConfirmed = true }; var createResult = await this.userManager.CreateAsync(user); if (!createResult.Succeeded) { return(BadRequest(createResult.Errors)); } } await this.userManager.AddLoginAsync(user, new UserLoginInfo(nameof(Provider.LDAP), ldapUser.Id.ToString(), Provider.LDAP)); await this.signInManager.SignInAsync(user, isPersistent : model.RememberMe); } return(Ok(model.ReturnUrl)); } } else { var user = await this.userManager.FindByNameAsync(model.UserName); if (user != null && await this.userManager.CheckPasswordAsync(user, model.Password)) { await this.signInManager.SignInAsync(user, isPersistent : model.RememberMe); return(Ok(model.ReturnUrl)); } } ModelState.AddModelError(string.Empty, AccountOptions.InvalidCredentialsErrorMessage); } return(BadRequest(ModelState)); }