コード例 #1
0
        public async Task <Result> Stop()
        {
            string impersonatorId = _identityUIUserInfoService.GetImpersonatorId();

            AppUserEntity appUser = await _userManager.FindByIdAsync(impersonatorId);

            if (appUser == null)
            {
                _logger.LogError($"Impersonator not found. ImpersonatorId {impersonatorId}");
                return(Result.Fail(USER_NOT_FOUND));
            }

            appUser.SessionCode = Guid.NewGuid().ToString();

            Result addSessionResult = await _sessionService.Add(appUser.SessionCode, appUser.Id);

            if (addSessionResult.Failure)
            {
                return(Result.Fail(FAILED_TO_ADD_SESSION));
            }

            string userId      = _identityUIUserInfoService.GetUserId();
            string sessionCode = _identityUIUserInfoService.GetSessionCode();

            await _signInManager.SignOutAsync();

            _sessionService.Logout(sessionCode, userId, SessionEndTypes.ImpersonationLogout);

            await _signInManager.SignInAsync(appUser, false); //TODO: save this when starting impersonating

            _logger.LogInformation($"User is stopped to impersonate another user. ImpersnonazerId {impersonatorId}, user to be impersonalized {userId}");

            return(Result.Ok());
        }
コード例 #2
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));
        }