Allows code to be executed under the security context of a specified user account.
Implements IDispose, so can be used via a using-directive or method calls; ... var imp = new Impersonator( "myUsername", "myDomainname", "myPassword" ); imp.UndoImpersonation(); ... var imp = new Impersonator(); imp.Impersonate("myUsername", "myDomainname", "myPassword"); imp.UndoImpersonation(); ... using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) ) { ... [code that executes under the new context] ... } ...
Наследование: IDisposable
Пример #1
0
        private void D3Prefs()
        {
            var imp = new Impersonator();
            if (Parent.UseWindowsUser)
                imp.Impersonate(Parent.WindowsUserName, "localhost", Parent.WindowsUserPassword);
            // Copy D3Prefs
            Logger.Instance.Write("Replacing D3Prefs for user: {0}", Environment.UserName);
            var currentprefs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                               @"\Diablo III\D3Prefs.txt";
            if (Directory.Exists(Path.GetDirectoryName(currentprefs)))
            {
                Logger.Instance.Write("Copy custom D3Prefs file to: {0}", currentprefs);
                try
                {
                    File.Copy(Parent.D3PrefsLocation, currentprefs, true);
                }
                catch (Exception ex)
                {
                    Logger.Instance.Write("Failed to copy D3Prefs file: {0}", ex);
                }
            }
            else
                Logger.Instance.Write("D3Prefs Failed: Path to \"{0}\" does not exist!", currentprefs);
            if (imp != null)
                imp.Dispose();

            // Also replace Default User D3Prefs
            var defaultprefs =
                Regex.Match(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
                            string.Format(@"(.+)\\{0}.*", Environment.UserName)).Groups[1].Value;
            if (Directory.Exists(defaultprefs + "\\Default"))
                defaultprefs += "\\Default";
            else if (Directory.Exists(defaultprefs + "\\Default User"))
                defaultprefs += "\\Default User";
            else
                return;
            defaultprefs += @"\Diablo III\D3Prefs.txt";
            if (Directory.Exists(Path.GetDirectoryName(defaultprefs)))
            {
                Logger.Instance.Write("Copy custom D3Prefs file to: {0}", defaultprefs);
                try
                {
                    File.Copy(Parent.D3PrefsLocation, defaultprefs, true);
                }
                catch (Exception ex)
                {
                    Logger.Instance.Write("Failed to copy d3prefs file: {0}", ex);
                }
            }
            Thread.Sleep(1000);
        }
Пример #2
0
        // Dont link this list
        public static void Create(BotClass bot)
        {
            var imp = new Impersonator();
            try
            {
                if (bot.UseWindowsUser)
                    imp.Impersonate(bot.WindowsUserName, "localhost", bot.WindowsUserPassword);

                bot.Status = "Create Diablo Clone";
                string basepath = Path.GetDirectoryName(bot.Diablo.Location);
                string clonepath = Path.Combine(bot.DiabloCloneLocation, "Diablo III");

                // if diablo base path does not exist stop here!
                if (basepath != null && !Directory.Exists(basepath))
                {
                    bot.Stop();
                    throw new Exception("Diablo base directory does not exist!");
                }

                // Check if given language is installed on basepath
                string testpath = Path.Combine(basepath, @"Data_D3\PC\MPQs", General.GetLocale(bot.Diablo.Language));
                if (!Directory.Exists(testpath))
                {
                    bot.Stop();
                    throw new Exception(string.Format("ERROR: {0} language is not installed (path: {1})",
                        bot.Diablo.Language, testpath));
                }

                // if diablo clone does not exist create it
                if (!Directory.Exists(Path.Combine(clonepath, @"Data_D3\PC\MPQs\Cache")))
                {
                    Logger.Instance.Write(bot, "Creating new Diablo Clone");
                    Directory.CreateDirectory(Path.Combine(clonepath, @"Data_D3\PC\MPQs\Cache"));
                }

                // Create Search caches
                var baseFileCache = new FileListCache(basepath);
                var cloneFileCache = new FileListCache(clonepath);

                // Check if all links are made for our clone
                foreach (FileListCache.MyFile p in baseFileCache.FileList)
                {
                    try
                    {
                        if (p.directory && !Directory.Exists(Path.Combine(clonepath.ToLower(), p.Path.ToLower())))
                        {
                            if (!_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                            {
                                Logger.Instance.Write(bot, "NewLink: {0} -> {1}", Path.Combine(clonepath, p.Path),
                                    Path.Combine(basepath, p.Path));
                                //if (!CreateSymbolicLink( Path.Combine(clonepath,p.Path),  Path.Combine(basepath,p.Path), 1))
                                //  throw new Exception("Failed to create link!");
                                Directory.CreateDirectory(Path.Combine(clonepath, p.Path));
                            }
                            continue;
                        }
                        if (!p.directory && !File.Exists(Path.Combine(clonepath.ToLower(), p.Path.ToLower())))
                        {
                            if (!_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                            {
                                Logger.Instance.Write(bot, "NewLink: {0} -> {1}", Path.Combine(clonepath, p.Path),
                                    Path.Combine(basepath, p.Path));
                                if (Path.GetExtension(Path.Combine(clonepath, p.Path)).ToLower().Equals(".exe"))
                                {
                                    if (
                                        !CreateHardLink(Path.Combine(clonepath, p.Path), Path.Combine(basepath, p.Path),
                                            IntPtr.Zero))
                                        throw new Exception("Failed to create link!");
                                }
                                else
                                {
                                    if (
                                        !CreateSymbolicLink(Path.Combine(clonepath, p.Path),
                                            Path.Combine(basepath, p.Path), 0))
                                        throw new Exception("Failed to create link!");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                // Remove links that have no target
                /*
                foreach (var p in cloneFileCache.FileList)
                {
                    try
                    {
                        if (p.directory && !Directory.Exists(Path.Combine(basepath, p.Path)))
                        {
                            if (!_noLinks.Any(n => General.WildcardMatch(n.Source.ToLower(), p.Path.ToLower())))
                                Console.WriteLine("Delete: {0}", p.Path);
                            continue;
                        }

                        if (!p.directory && !File.Exists(Path.Combine(basepath.ToLower(), p.Path.ToLower())))
                        {
                            if (!_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                                Console.WriteLine("Delete: {0}", p.Path);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.Write(bot, ex.ToString());
                    }
                }
                 */
            }
            catch (Exception ex)
            {
                bot.Stop();
                DebugHelper.Write(bot, "Failed to create clone!");
                DebugHelper.Exception(ex);
            }
            imp.Dispose();
        }