public async Task SetPassword(LogonRequestData logonRequestData, string newPassword)
        {
            try
            {
                await _client.ChangePasswordAsync(new nsk.ChangePasswordRequest
                {
                    logonRequestData = Mapper.Map <nsk.LogonRequestData>(logonRequestData),
                    ContractVersion  = _newskiesSettings.ApiContractVersion,
                    newPassword      = newPassword
                });
            }
            catch (System.ServiceModel.FaultException e)
            {
                //"FaultException: The agent (WW2/[email protected]) was not authenticated."
                if (e.Message.Contains("The agent") && e.Message.Contains("was not authenticated"))
                {
                    throw new ResponseErrorException(ResponseErrorCode.InvalidCurrentPassword, "Invalid current password. ");
                }

                //"FaultException: The agent (WW2/[email protected]) tried to set the same password."
                if (e.Message.Contains("The agent") && e.Message.Contains("tried to set the same password"))
                {
                    throw new ResponseErrorException(ResponseErrorCode.InvalidNewPassword, "Invalid new password. ");
                }

                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public UserSessionService(IMemoryCache cache, ISessionBagService sessionBag, IAgentManager agentClient, IPersonManager personClient,
                                  nsk.ISessionManager client, IMapper mapper, IHttpContextAccessor httpContextAccessor, IOptions <AppSettings> appSettings)
            : base(appSettings)
        {
            _cache               = cache ?? throw new ArgumentNullException(nameof(cache));
            _sessionBag          = sessionBag ?? throw new ArgumentNullException(nameof(sessionBag));
            _agentClient         = agentClient ?? throw new ArgumentNullException(nameof(agentClient));
            _personClient        = personClient ?? throw new ArgumentNullException(nameof(personClient));
            _client              = client ?? throw new ArgumentNullException(nameof(client));
            _mapper              = mapper ?? throw new ArgumentNullException(nameof(mapper));
            _defLogonRequestData = new LogonRequestData
            {
                DomainCode = _newskiesSettings.AgentDomain,
                AgentName  = _newskiesSettings.AnonymousAgentName,
                Password   = _newskiesSettings.AnonymousAgentPassword,
                RoleCode   = _newskiesSettings.AnonymousAgentRole
            };
            var localIpAddress = Task.Run(async() => await httpContextAccessor.HttpContext.GetServerIP()).Result;

            _sharedSignatureCacheKey = "_sharedSignature_" + localIpAddress.ToString();
            _newskiesIdleTimeout     = appSettings.Value.ApplicationSessionOptions != null ?
                                       appSettings.Value.ApplicationSessionOptions.NewskiesIdleTimeout :
                                       throw new ArgumentNullException(nameof(appSettings.Value.ApplicationSessionOptions));
        }
        public async Task <LogonResponse> Logon(LogonRequestData logonRequestData)
        {
            if (!string.IsNullOrEmpty(await _sessionBag.AgentName()))
            {
                throw new ResponseErrorException(ResponseErrorCode.AlreadyLoggedIn,
                                                 string.Format("Already logged in as {0}", await _sessionBag.AgentName()));
            }
            nsk.LogonResponse logonResponse;
            var previousSignature = await _sessionBag.Signature();

            try
            {
                logonResponse = await _client.LogonAsync(new nsk.LogonRequest
                {
                    logonRequestData = _mapper.Map <nsk.LogonRequestData>(logonRequestData)
                });
            }
            catch (System.ServiceModel.FaultException e)
            {
                if (e.Message.Contains("No agent found") || e.Message.Contains("was not authenticated") || e.Message.StartsWith("Unable to find best role for agent"))
                {
                    throw new ResponseErrorException(ResponseErrorCode.InvalidLogin, "Invalid login details. ");
                }

                //Handle scenario: System.ServiceModel.FaultException: 'The agent (WW2/[email protected]) must reset their password.'
                if (e.Message.Contains("must reset their password"))
                {
                    return(new LogonResponse {
                        MustChangePassword = true
                    });
                }

                throw e;
            }
            var newSignature = logonResponse.Signature;
            var booking      = await _sessionBag.Booking();

            if (booking != null && string.IsNullOrEmpty(booking.RecordLocator) && !string.IsNullOrEmpty(previousSignature))
            {
                var result = await _client.TransferSessionAsync(new nsk.TransferSessionRequest
                {
                    ContractVersion = _newskiesSettings.ApiContractVersion,
                    tokenRequest    = new nsk.TokenRequest
                    {
                        Token       = previousSignature,
                        ChannelType = nsk.ChannelType.API,
                        SystemType  = nsk.SystemType.WebServicesAPI
                    }
                });

                newSignature = result.TransferSessionResponseData.Signature;
            }
            await _sessionBag.SetSignature(newSignature);

            //await _sessionBag.SetSignature(logonResponse.Signature);
            await _sessionBag.SetAgentName(logonRequestData.AgentName);

            await _sessionBag.SetAgentPassword(logonRequestData.Password);

            var agentInfo = await GetAgentInfo(logonRequestData.AgentName, logonRequestData.DomainCode);

            await _sessionBag.SetRoleCode(agentInfo.Item1);

            await _sessionBag.SetOrganizationCode(agentInfo.Item2);

            await _sessionBag.SetAgentId(agentInfo.Item3);

            await _sessionBag.SetPersonId(agentInfo.Item4);

            await _sessionBag.SetCustomerNumber(agentInfo.Item5);

            return(_mapper.Map <LogonResponse>(logonResponse));
        }
 public async Task <IActionResult> Logon([FromBody] LogonRequestData logonRequestData)
 {
     return(new OkObjectResult(await _userSessionService.Logon(logonRequestData)));
 }