예제 #1
0
        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));
        }
예제 #2
0
        public async Task <Core.Models.Result.Result> ExternalLoginRequest(ExternalLoginRegisterRequest externalLoginRegisterRequest)
        {
            if (!_identityUIEndpoints.RegisterEnabled)
            {
                _logger.LogError($"User tried to register, but registrations are disabled");
                return(Core.Models.Result.Result.Fail("registration_is_not_enabled", "Registration disabled"));
            }

            ValidationResult externalLoginValidationResult = _externalLoginRequsterRequestValidator.Validate(externalLoginRegisterRequest);

            if (!externalLoginValidationResult.IsValid)
            {
                _logger.LogWarning($"Invalid {typeof(ExternalLoginRegisterRequest)} model");
                return(Core.Models.Result.Result.Fail(externalLoginValidationResult.Errors));
            }

            ExternalLoginInfo externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync();

            if (externalLoginInfo == null)
            {
                _logger.LogError($"Failed to get external login info.");
                return(Core.Models.Result.Result.Fail("failed_to_get_external_login_info", "Failed to get external login info"));
            }

            //TODO: do not confirm. don't require confirmed emails for external login users
            CommonUtils.Result.Result <AppUserEntity> addUserResult = await AddUser(
                externalLoginRegisterRequest,
                setPassword : false,
                sendConfirmationMail : false,
                emailConfirmed : true);

            if (addUserResult.Failure)
            {
                return(addUserResult.ToOldResult());
            }

            AppUserEntity appUser = addUserResult.Value;

            IdentityResult addLoginResult = await _userManager.AddLoginAsync(appUser, externalLoginInfo);

            if (!addLoginResult.Succeeded)
            {
                _logger.LogError($"Failed to add login to user. UserId {appUser.Id}");
            }

            return(Core.Models.Result.Result.Ok());
        }