コード例 #1
0
        /// <summary>
        /// Saves Crm connections list to file
        /// </summary>
        public void SaveConnectionsFile(CrmConnections connectionsList)
        {
            if (!string.IsNullOrEmpty(connectionsList.Password))
            {
                connectionsList.Password = CryptoManager.Encrypt(connectionsList.Password,
                                                                 CryptoPassPhrase,
                                                                 CryptoSaltValue,
                                                                 CryptoHashAlgorythm,
                                                                 CryptoPasswordIterations,
                                                                 CryptoInitVector,
                                                                 CryptoKeySize);
            }

            var cache = new Dictionary <Guid, string>();

            foreach (var detail in connectionsList.Connections)
            {
                if (!detail.ConnectionId.HasValue)
                {
                    continue;
                }

                cache.Add(detail.ConnectionId.Value, detail.UserPassword);

                if (detail.SavePassword)
                {
                    if (!string.IsNullOrEmpty(detail.UserPassword))
                    {
                        detail.UserPassword = CryptoManager.Encrypt(detail.UserPassword,
                                                                    CryptoPassPhrase,
                                                                    CryptoSaltValue,
                                                                    CryptoHashAlgorythm,
                                                                    CryptoPasswordIterations,
                                                                    CryptoInitVector,
                                                                    CryptoKeySize);
                    }
                }
                else
                {
                    detail.UserPassword = null;
                }
            }

            XmlSerializerHelper.SerializeToFile(connectionsList, ConfigFileName);

            foreach (var detail in connectionsList.Connections)
            {
                if (!detail.ConnectionId.HasValue)
                {
                    continue;
                }

                if (detail.UserPassword == null)
                {
                    detail.UserPassword = cache[detail.ConnectionId.Value];
                    continue;
                }

                if (!string.IsNullOrEmpty(detail.UserPassword))
                {
                    detail.UserPassword = CryptoManager.Decrypt(detail.UserPassword,
                                                                CryptoPassPhrase,
                                                                CryptoSaltValue,
                                                                CryptoHashAlgorythm,
                                                                CryptoPasswordIterations,
                                                                CryptoInitVector,
                                                                CryptoKeySize);
                }
            }
        }
コード例 #2
0
 public void Save()
 {
     XmlSerializerHelper.SerializeToFile(instance, Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, "MscrmTools.ConnectionsList.xml"));
 }
コード例 #3
0
        /// <summary>
        /// Restore Crm connections list from the file
        /// </summary>
        /// <returns>List of Crm connections</returns>
        public CrmConnections LoadConnectionsList()
        {
            CrmConnections crmConnections;

            try
            {
                if (File.Exists(ConfigFileName))
                {
                    using (var configReader = new StreamReader(ConfigFileName))
                    {
                        crmConnections = (CrmConnections)XmlSerializerHelper.Deserialize(configReader.ReadToEnd(), typeof(CrmConnections));
                    }

                    if (!string.IsNullOrEmpty(crmConnections.Password))
                    {
                        crmConnections.Password = CryptoManager.Decrypt(crmConnections.Password,
                                                                        CryptoPassPhrase,
                                                                        CryptoSaltValue,
                                                                        CryptoHashAlgorythm,
                                                                        CryptoPasswordIterations,
                                                                        CryptoInitVector,
                                                                        CryptoKeySize);
                    }

                    foreach (var detail in crmConnections.Connections)
                    {
                        if (!string.IsNullOrEmpty(detail.UserPassword))
                        {
                            detail.UserPassword = CryptoManager.Decrypt(detail.UserPassword,
                                                                        CryptoPassPhrase,
                                                                        CryptoSaltValue,
                                                                        CryptoHashAlgorythm,
                                                                        CryptoPasswordIterations,
                                                                        CryptoInitVector,
                                                                        CryptoKeySize);
                        }

                        // Fix for new connection code
                        if (string.IsNullOrEmpty(detail.OrganizationUrlName))
                        {
                            if (detail.UseIfd || detail.UseOnline || detail.UseOsdp)
                            {
                                var uri = new Uri(detail.OrganizationServiceUrl);
                                detail.OrganizationUrlName = uri.Host.Split('.')[0];
                            }
                            else
                            {
                                detail.OrganizationUrlName = detail.Organization;
                            }
                        }

                        // Fix old connection for TimeOut
                        if (detail.Timeout == TimeSpan.Zero)
                        {
                            detail.Timeout = new TimeSpan(1200000000);
                        }
                    }
                }
                else
                {
                    crmConnections = new CrmConnections
                    {
                        Connections = new List <ConnectionDetail>()
                    };
                }

                return(crmConnections);
            }
            catch (Exception error)
            {
                throw new Exception("Error while deserializing configuration file. Details: " + error.Message);
            }
        }