private Task PerformRedirectionBasedOnStatus(TokenValidatedContext context)
        {
            var odsCode             = context.Principal.GetClaimValue("ODS");
            var organisationDetails = _sdsQueryExecutionBase.GetOrganisationDetailsByOdsCode(odsCode).Result;

            if (organisationDetails != null)
            {
                var organisation = _applicationService.GetOrganisation(organisationDetails.OdsCode);

                if (organisation != null)
                {
                    var emailAddress = StringExtensions.Coalesce(context.Principal.GetClaimValue("Email"), context.Principal.GetClaimValue("Email Address"));
                    var user         = _applicationService.GetUser(emailAddress);

                    if (user != null)
                    {
                        switch ((UserAccountStatus)user.UserAccountStatusId)
                        {
                        case UserAccountStatus.Authorised:
                            var loggedOnUser = LogonAuthorisedUser(emailAddress, context, organisation);
                            PopulateAdditionalClaims((UserAccountStatus)user.UserAccountStatusId, loggedOnUser, emailAddress, context, organisation);
                            context.Properties.RedirectUri = GetAuthorisedRedirectUri(context.Properties.RedirectUri);
                            break;

                        case UserAccountStatus.Pending:
                            PopulateAdditionalClaims((UserAccountStatus)user.UserAccountStatusId, null, emailAddress, context, organisation);
                            context.Properties.RedirectUri = "/PendingAccount";
                            break;

                        case UserAccountStatus.Deauthorised:
                        case UserAccountStatus.RequestDenied:
                            PopulateAdditionalClaims((UserAccountStatus)user.UserAccountStatusId, null, emailAddress, context, organisation);
                            context.Properties.RedirectUri = "/SubmitUserForm";
                            break;
                        }
                    }
                    else
                    {
                        PopulateAdditionalClaims(null, null, emailAddress, context, organisation);
                        context.Properties.RedirectUri = GetAuthorisedRedirectUriForRegistration(context.Properties.RedirectUri);
                    }
                }
                else
                {
                    context.Properties.RedirectUri = "/";
                }
            }
            return(Task.CompletedTask);
        }
        private async Task GetSearchResults()
        {
            try
            {
                var providerOrganisationDetails = await _sdsQueryExecutionBase.GetOrganisationDetailsByOdsCode(ProviderOdsCode);

                var consumerOrganisationDetails = await _sdsQueryExecutionBase.GetOrganisationDetailsByOdsCode(ConsumerOdsCode);

                _auditSearchParameters[0] = ConsumerOdsCode;
                _auditSearchParameters[1] = ProviderOdsCode;
                _auditSearchParameters[2] = SelectedDateRange;
                _auditSearchParameters[3] = SelectedOrganisationType;

                ProviderODSCodeFound = providerOrganisationDetails != null;
                ConsumerODSCodeFound = consumerOrganisationDetails != null;

                if (ProviderODSCodeFound && (ConsumerODSCodeFound || SelectedOrganisationType != null))
                {
                    var providerSpineDetails = await _sdsQueryExecutionBase.GetProviderDetails(ProviderOdsCode);

                    var consumerSpineDetails = await _sdsQueryExecutionBase.GetConsumerDetails(ConsumerOdsCode);

                    ProviderEnabledForGpConnectAppointmentManagement = providerSpineDetails != null;
                    ConsumerEnabledForGpConnectAppointmentManagement = (consumerSpineDetails != null && consumerSpineDetails.HasAsId) || SelectedOrganisationType != null;

                    if (ProviderEnabledForGpConnectAppointmentManagement)
                    {
                        ProviderASIDPresent = providerSpineDetails.HasAsId;

                        if (ProviderASIDPresent)
                        {
                            await PopulateSearchResults(providerSpineDetails, providerOrganisationDetails, consumerSpineDetails, consumerOrganisationDetails, SelectedOrganisationType);

                            SearchAtResultsText         = providerOrganisationDetails.FormattedOrganisationDetails;
                            SearchOnBehalfOfResultsText = GetSearchOnBehalfOfResultsText(consumerOrganisationDetails?.FormattedOrganisationDetails, SelectedOrganisationType);
                            ProviderPublisher           = providerSpineDetails.ProductName;
                        }
                        else
                        {
                            _auditSearchIssues.Add(SearchConstants.ISSUEWITHGPCONNECTPROVIDERTEXT);
                        }
                    }
                    else
                    {
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHGPCONNECTPROVIDERNOTENABLEDTEXT, ProviderOdsCode));
                    }
                }
                else
                {
                    if (!ProviderODSCodeFound)
                    {
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHPROVIDERODSCODETEXT, ProviderOdsCode));
                    }
                    if (!ConsumerODSCodeFound)
                    {
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHCONSUMERODSCODETEXT, ConsumerOdsCode));
                    }
                }
            }
            catch (LdapException)
            {
                LdapErrorRaised = true;
                _auditSearchIssues.Add(SearchConstants.ISSUEWITHLDAPTEXT);
            }
        }