public string GetOrganizationCrmConnectionString()
        {
            var currentServerName = string.Empty;

            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);
            }

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

            //append timeout in seconds to connectionstring
            connectionString += string.Format("Timeout={0};", Timeout.ToString(@"hh\:mm\:ss"));
            return(connectionString);
        }
        private string GetOrganizationCrmConnectionString()
        {
            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);
                    }
                }

                if (string.IsNullOrEmpty(userPassword))
                {
                    throw new Exception("User password cannot be null. If the user password is not stored in configuration file, you should request it from the end user");
                }

                var decryptedPassword = CryptoManager.Decrypt(userPassword, ConnectionManager.CryptoPassPhrase,
                                                              ConnectionManager.CryptoSaltValue,
                                                              ConnectionManager.CryptoHashAlgorythm,
                                                              ConnectionManager.CryptoPasswordIterations,
                                                              ConnectionManager.CryptoInitVector,
                                                              ConnectionManager.CryptoKeySize);

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

            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);
            }

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

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