public static OrganizationListing GetOrganization(string token, string apiVersion, string organizationName, string serverUrl, List <OrganizationListing> orgList)
        {
            var apiInstance = new OrganizationsApi(serverUrl);

            apiInstance.Configuration.AccessToken = token;
            OrganizationListing organization = null;

            try
            {
                if (string.IsNullOrEmpty(organizationName))
                {
                    if (orgList.Count == 1)
                    {
                        organization = orgList.FirstOrDefault();
                    }

                    if (orgList.Count == 0)
                    {
                        throw new Exception("No Organizations were found for the current user");
                    }
                    if (orgList.Count > 1)
                    {
                        throw new Exception("Multiple Organizations exist for the current user, please specify an Organization name");
                    }
                }
                else
                {
                    bool IsUserInOrg = false;
                    foreach (var org in orgList)
                    {
                        if (org.Name == organizationName)
                        {
                            IsUserInOrg  = true;
                            organization = org;
                            break;
                        }
                    }

                    if (!IsUserInOrg)
                    {
                        throw new Exception($"Organization {organizationName} does not match user's existing organizations");
                    }
                }

                return(organization);
            }
            catch (Exception ex)
            {
                if (ex.Message != "One or more errors occurred.")
                {
                    throw new InvalidOperationException("Exception when calling OrganizationsApi.GetOrganizations: " + ex.Message);
                }
                else
                {
                    throw new InvalidOperationException(ex.InnerException.Message);
                }
            }
        }
        public UserInfo GetUserInfo(string apiVersion, string serverType, string organizationName, string environment, string serverUrl, string username, string password)
        {
            ServerInfo          serverInfo   = new ServerInfo();
            OrganizationListing organization = new OrganizationListing();
            string loginUrl     = serverUrl;
            string documentsUrl = string.Empty;

            if (serverType == "Cloud" || serverType == "Documents")
            {
                var serviceRegistrationList = GetServiceRegistration(apiVersion, environment);

                if (serviceRegistrationList == null || serviceRegistrationList.Count() == 0)
                {
                    throw new Exception("Service registration could not be found");
                }

                foreach (var serviceRegistration in serviceRegistrationList)
                {
                    if (serviceRegistration.ServiceTag == "OpenBots" && serverType == "Cloud") // Authentication
                    {
                        if (serviceRegistration.IsCurrentlyUnderMaintenance)
                        {
                            throw new Exception($"Server {serviceRegistration.Name} is currently undergoing maintenance and cannot be accessed at this time");
                        }
                        else
                        {
                            loginUrl = serviceRegistration.ServiceBaseUri.ToString();
                        }
                    }

                    if (serviceRegistration.ServiceTag == "OpenBots.CloudServer" && serverType == "Cloud") // CloudServer Orchestration API
                    {
                        if (serviceRegistration.IsCurrentlyUnderMaintenance)
                        {
                            throw new Exception($"Server {serviceRegistration.Name} is currently undergoing maintenance and cannot be accessed at this time");
                        }
                        else
                        {
                            serverUrl = serviceRegistration.ServiceBaseUri.ToString();
                        }
                    }
                    if (serviceRegistration.ServiceTag == "OpenBots.Documents" && serverType == "Documents")
                    {
                        if (serviceRegistration.IsCurrentlyUnderMaintenance)
                        {
                            throw new Exception($"Server {serviceRegistration.Name} is currently undergoing maintenance and cannot be accessed at this time");
                        }
                        else
                        {
                            documentsUrl = serviceRegistration.ServiceBaseUri.ToString();
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(serverUrl))
            {
                throw new Exception("Server URL not found");
            }

            if ((serverType == "Cloud" || serverType == "Local") && (username == null || password == null))
            {
                throw new Exception("Agent credentials not found in registry");
            }
            else if (serverType == "Documents" && (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)))
            {
                throw new Exception("Credential values are null or an empty string");
            }


            string token = GetAuthToken(apiVersion, serverType, username, password, loginUrl);

            if (serverType == "Cloud")
            {
                serverInfo   = GetServerInfo(apiVersion, serverUrl, token);
                organization = GetOrganization(token, apiVersion, organizationName, serverUrl, serverInfo.MyOrganizations);
            }

            var userInfo = new UserInfo()
            {
                OrganizationId   = organization?.Id.ToString(),
                OrganizationName = organization?.Name,
                ServerType       = serverType,
                Token            = token,
                ServerUrl        = serverUrl,
                LoginUrl         = loginUrl,
                DocumentsUrl     = documentsUrl,
                ApiVersion       = apiVersion,
                Environment      = environment,
                UserId           = serverInfo?.PersonId.ToString()
            };

            return(userInfo);
        }