public string GetDiscoveryCrmConnectionString()
        {
            var connectionString = string.Format("Url={0}://{1}:{2};",
                                                 UseSsl ? "https" : "http",
                                                 UseIfd ? ServerName : UseOsdp ? "disco." + ServerName : UseOnline ? "dev." + ServerName : ServerName,
                                                 ServerPort == 0 ? (UseSsl ? 443 : 80) : ServerPort);

            if (IsCustomAuth)
            {
                if (!UseIfd)
                {
                    if (!string.IsNullOrEmpty(UserDomain))
                    {
                        connectionString += string.Format("Domain={0};", UserDomain);
                    }
                }

                string username = UserName;
                if (UseIfd)
                {
                    if (!string.IsNullOrEmpty(UserDomain))
                    {
                        username = string.Format("{0}\\{1}", UserDomain, UserName);
                    }
                }

                connectionString += string.Format("Username={0};Password={1};", username, UserPassword);
            }

            if (UseOnline && !UseOsdp)
            {
                ClientCredentials deviceCredentials;

                do
                {
                    deviceCredentials = DeviceIdManager.LoadDeviceCredentials() ??
                                        DeviceIdManager.RegisterDevice();
                } while (deviceCredentials.UserName.Password.Contains(";") ||
                         deviceCredentials.UserName.Password.Contains("=") ||
                         deviceCredentials.UserName.Password.Contains(" ") ||
                         deviceCredentials.UserName.UserName.Contains(";") ||
                         deviceCredentials.UserName.UserName.Contains("=") ||
                         deviceCredentials.UserName.UserName.Contains(" "));

                connectionString += string.Format("DeviceID={0};DevicePassword={1};",
                                                  deviceCredentials.UserName.UserName,
                                                  deviceCredentials.UserName.Password);
            }

            if (UseIfd && !string.IsNullOrEmpty(HomeRealmUrl))
            {
                connectionString += string.Format("HomeRealmUri={0};", HomeRealmUrl);
            }

            return(connectionString);
        }
        public string GetOrganizationCrmConnectionString()
        {
            var currentServerName = string.Empty;
            var authType          = "AD";

            if (UseOsdp || UseOnline)
            {
                currentServerName = string.Format("{0}.{1}", OrganizationUrlName, ServerName);
            }
            else if (UseIfd)
            {
                var serverNameParts = ServerName.Split('.');

                serverNameParts[0] = OrganizationUrlName;


                currentServerName = string.Format("{0}:{1}",
                                                  string.Join(".", serverNameParts),
                                                  ServerPort == 0 ? (UseSsl ? 443 : 80) : ServerPort);
            }
            else
            {
                currentServerName = string.Format("{0}:{1}/{2}",
                                                  ServerName,
                                                  ServerPort == 0 ? (UseSsl ? 443 : 80) : ServerPort,
                                                  Organization);
            }

            var connectionString = string.Format("Url={0}://{1};",
                                                 UseSsl ? "https" : "http",
                                                 currentServerName);

            //var connectionString = string.Format("Url={0};", OrganizationServiceUrl.Replace("/XRMServices/2011/Organization.svc", ""));

            if (IsCustomAuth)
            {
                if (!UseIfd)
                {
                    if (!string.IsNullOrEmpty(UserDomain))
                    {
                        connectionString += string.Format("Domain={0};", UserDomain);
                    }
                }

                string username = UserName;
                if (UseIfd)
                {
                    if (!string.IsNullOrEmpty(UserDomain))
                    {
                        username = string.Format("{0}\\{1}", UserDomain, UserName);
                    }
                }

                connectionString += string.Format("Username={0};Password={1};", username, UserPassword);
            }

            if (UseOnline)
            {
                ClientCredentials deviceCredentials;

                do
                {
                    deviceCredentials = DeviceIdManager.LoadDeviceCredentials() ??
                                        DeviceIdManager.RegisterDevice();
                } while (deviceCredentials.UserName.Password.Contains(";") ||
                         deviceCredentials.UserName.Password.Contains("=") ||
                         deviceCredentials.UserName.Password.Contains(" ") ||
                         deviceCredentials.UserName.UserName.Contains(";") ||
                         deviceCredentials.UserName.UserName.Contains("=") ||
                         deviceCredentials.UserName.UserName.Contains(" "));

                connectionString += string.Format("DeviceID={0};DevicePassword={1};",
                                                  deviceCredentials.UserName.UserName,
                                                  deviceCredentials.UserName.Password);

                authType = "Office365";
            }

            if (UseIfd && !string.IsNullOrEmpty(HomeRealmUrl))
            {
                connectionString += string.Format("HomeRealmUri={0};", HomeRealmUrl);
            }
            if (UseIfd)
            {
                authType = "IFD";
            }
            connectionString += string.Format("AuthType={0};", authType);

            //append timeout in seconds to connectionstring
            connectionString += string.Format("Timeout={0};", Timeout.ToString(@"hh\:mm\:ss"));
            return(connectionString);
        }