public async Task <Result <SignInResult> > Callback(string remoteError) { if (!string.IsNullOrEmpty(remoteError)) { _logger.LogError($"External login provider returned error. Error {remoteError}"); return(Result.Fail <SignInResult>("external_login_provider_error", remoteError)); } ExternalLoginInfo externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync(); if (externalLoginInfo == null) { _logger.LogError($"Error getting external login info"); return(Result.Fail <SignInResult>("failed_to_get_external_longin_info", "Failed to get external login info")); } AppUserEntity appUser = await _userManager.FindByLoginAsync(externalLoginInfo.LoginProvider, externalLoginInfo.ProviderKey); if (appUser == null) { _logger.LogInformation($"Users email does not exist"); return(Result.Ok(SignInResult.Failed)); } string sessionCode = _identityUIUserInfoService.GetSessionCode(); if (sessionCode != null) { _sessionService.Logout(sessionCode, appUser.Id, SessionEndTypes.Expired); } CommonUtils.Result.Result beforeLoginFilterResult = await _canLoginService.BeforeAdd(appUser); if (beforeLoginFilterResult.Failure) { _logger.LogInformation($"User is not allowed to login. User {appUser.Id}"); beforeLoginFilterResult.ToOldResult(); } SignInResult signInResult = await _signInManager.ExternalLoginSignInAsync( loginProvider : externalLoginInfo.LoginProvider, providerKey : externalLoginInfo.ProviderKey, isPersistent : false, bypassTwoFactor : _identityUIEndpoints.BypassTwoFactorOnExternalLogin); CommonUtils.Result.Result afterLoginFilterResult = await _canLoginService.AfterAdded(appUser); if (afterLoginFilterResult.Failure) { await _signInManager.SignOutAsync(); _sessionService.Logout(appUser.SessionCode, appUser.Id, SessionEndTypes.AffterLoginFilterFailure); _logger.LogInformation($"User is not allowed to login. User {appUser.Id}"); afterLoginFilterResult.ToOldResult(); } return(Result.Ok(signInResult)); }
public async Task <SignInResult> Login(string ip, string sessionCode, LoginRequest login) { ValidationResult validationResult = _loginValidator.Validate(login); if (!validationResult.IsValid) { _logger.LogError($"Invalid LoginRequest. UserName {login?.UserName}"); return(SignInResult.Failed); } await _signInManager.SignOutAsync(); AppUserEntity appUser = await _userManager.FindByNameAsync(login.UserName); if (appUser == null) { _logger.LogInformation($"No user with username {login.UserName}"); return(SignInResult.Failed); } if (sessionCode != null) { _sessionService.Logout(sessionCode, appUser.Id, SessionEndTypes.Expired); } CommonUtils.Result.Result beforeLoginfilterResult = await _canLoginService.BeforeAdd(appUser); if (beforeLoginfilterResult.Failure) { _logger.LogInformation($"User is not allowed to login. User {appUser.Id}"); return(SignInResult.Failed); } appUser.SessionCode = Guid.NewGuid().ToString(); Result addSessionResult = _sessionService.Add(appUser.SessionCode, appUser.Id, ip); if (addSessionResult.Failure) { return(SignInResult.Failed); } SignInResult result = await _signInManager.PasswordSignInAsync(appUser, login.Password, login.RememberMe, lockoutOnFailure : true); if (!result.Succeeded) { if (result.RequiresTwoFactor) { _logger.LogInformation($"Login Requires TwoFactor. User {appUser.Id}"); _sessionService.Logout(appUser.SessionCode, appUser.Id, SessionEndTypes.TwoFactorLogin); } if (!result.IsLockedOut) { _logger.LogInformation($"Failed to log in user. User {appUser.Id}"); _sessionService.Logout(appUser.SessionCode, appUser.Id, SessionEndTypes.InvalidLogin); } return(result); } CommonUtils.Result.Result afterLoginFilterResult = await _canLoginService.AfterAdded(appUser); if (afterLoginFilterResult.Failure) { await _signInManager.SignOutAsync(); _sessionService.Logout(appUser.SessionCode, appUser.Id, SessionEndTypes.AffterLoginFilterFailure); return(SignInResult.Failed); } _logger.LogInformation($"User id logged in. UserId {appUser.Id}"); return(result); }