public static IDisposable ImpersonateUser(RemoteSystemInfo systemInfo)
        {
            Impersonator impersonator = new Impersonator();
            try
            {
                if (systemInfo != null)
                {
                    var loggedInUser = LoggedOnUser.LogonUser(systemInfo.Username, systemInfo.Password);
                    if (loggedInUser != null)
                    {
                        impersonator._impersonationContext = WindowsIdentity.Impersonate(loggedInUser.DangerousGetHandle());
                        impersonator._impersonatedUser = loggedInUser.Username;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return impersonator;
        }
        public static IDisposable ImpersonateUser(RemoteSystemInfo systemInfo)
        {
            Impersonator impersonator = new Impersonator();

            try
            {
                if (systemInfo != null)
                {
                    var loggedInUser = LoggedOnUser.LogonUser(systemInfo.Username, systemInfo.Password);
                    if (loggedInUser != null)
                    {
                        impersonator._impersonationContext = WindowsIdentity.Impersonate(loggedInUser.DangerousGetHandle());
                        impersonator._impersonatedUser     = loggedInUser.Username;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(impersonator);
        }
Пример #3
0
        public bool PublishDatabase(DeploymentBaseOptions sourceBaseOptions, DeploymentBaseOptions destBaseOptions, string provider, bool logException)
        {
            bool publishSucceeded = true;

            this.LogTrace("Starting db publish for {0}", _localSite.SiteName);
            try
            {
                var providerOption = new DeploymentProviderOptions(provider);
                foreach (var providerSetting in providerOption.ProviderSettings)
                {
                    var setting = ConfigurationManager.AppSettings[providerSetting.Name];
                    if (!string.IsNullOrEmpty(setting))
                    {
                        try
                        {
                            providerSetting.Value = setting;
                        }
                        catch (Exception ex)
                        {
                            this.LogTrace("Error trying to set value: {0} for setting: {1}, {2}", setting, providerSetting.Name, ex.ToString());
                        }
                    }
                }

                string path = _localSite.Databases[0].DBConnectionString;
                if (path.Contains("|DataDirectory|"))
                {
                    // TODO: is there a better way to do this? Can we lookup |DataDirectory|
                    path = path.Replace("|DataDirectory|", this._localSite.PhysicalPath + @"\App_Data\");
                }

                providerOption.Path = path;
                RemoteSystemInfo remoteSystemInfo = null;
                if (RemoteSystemInfos.Servers.Any() && UseTrustedConnection(_localSite.Databases[0].DBConnectionString))
                {
                    remoteSystemInfo = RemoteSystemInfos.Servers[_localSite.ServerName];
                }

                using (Impersonator.ImpersonateUser(remoteSystemInfo))
                {
                    using (var sourceObject =
                               DeploymentManager.CreateObject(providerOption,
                                                              sourceBaseOptions))
                    {
                        LogTrace("Starting DB Sync for: {0} using {1}",
                                 (string)_localSite.Databases[0].DbConnectionStringBuilder["Initial Catalog"], provider);
                        this.LogTrace("Publishing database as: {0}", destBaseOptions.UserName);

                        try
                        {
                            sourceObject.SyncTo(provider, _publishSettings.SqlDBConnectionString.ConnectionString,
                                                destBaseOptions, new DeploymentSyncOptions());
                            this.LogTrace("DB Synced successfully");
                            Helper.UpdateStatus(_localSite.SiteName, _localSite.ServerName, true);
                        }
                        catch (Exception ex)
                        {
                            if (logException)
                            {
                                LogTrace("Error starting publish for db: {0}",
                                         (string)_localSite.Databases[0].DbConnectionStringBuilder["Initial Catalog"]);
                                this.LogTrace(ex.ToString());
                            }

                            publishSucceeded = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (logException)
                {
                    LogTrace("Error syncing db: {0}",
                             (string)_localSite.Databases[0].DbConnectionStringBuilder["Initial Catalog"]);
                    LogTrace(ex.ToString());
                }

                publishSucceeded = false;
            }

            return(publishSucceeded);
        }