HasAdministratorPrivileges() публичный Метод

public HasAdministratorPrivileges ( ) : bool
Результат bool
Пример #1
0
        public void Execute(WebsiteContext context)
        {
            if (context.SkipHosts) {
                Logger.Log("Will skip adding HOSTS entry.");
                return;
            }

            string host = new Uri(context.ProjectUrl).Authority;

            if (host == "localhost") {
                Logger.Log("Skip because host is \"localhost\".");
                return;
            }

            try {
                var hostsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts");

                var hostLines = File.ReadAllLines(hostsPath);
                var hostAlreadyAdded = hostLines.Any(h => h.Contains(host));

                if (hostAlreadyAdded) {
                    Logger.Warn("No change needed.");
                    return;
                }

                if (!context.HasAdministratorPrivileges()) {
                    Logger.Error("Could not execute Hosts task without administrator rights.");
                    return;
                }

                using (StreamWriter sw = File.AppendText(hostsPath)) {
                    string prefix = "";

                    if (!string.IsNullOrWhiteSpace(hostLines.Last()))
                        prefix = Environment.NewLine;

                    sw.Write("{1}127.0.0.1\t\t{0}", host, prefix);
                }
                Logger.Success("Successfully added HOSTS entry.");
            }
            catch (Exception ex) {
                Logger.Error("Failed to add HOSTS entry. " + ex);
            }
        }
Пример #2
0
        public void Execute(WebsiteContext context)
        {
            if (!context.HasAdministratorPrivileges()) {
                Logger.Error("Could not execute Webserver task without administrator rights.");
                return;
            }

            using (ServerManager manager = new ServerManager()) {
                var webProjectDirectory = context.GetWebProjectDirectory();
                var siteAlreadyExists = manager.Sites.Any(s => s.Applications.Any(app => app.VirtualDirectories.Any(dir => dir.PhysicalPath == webProjectDirectory)));

                if (siteAlreadyExists) {
                    Logger.Warn("Site already exists in IIS.");
                    return;
                }

                var name = context.ProjectName;
                var host = new Uri(context.ProjectUrl).Host;

                // Create appool with project name
                var appPool = manager.ApplicationPools.Add(name);
                appPool.ManagedRuntimeVersion = GetFrameworkVersion(context);

                appPool.ProcessModel.PingingEnabled = false;
                appPool.Enable32BitAppOnWin64 = context.Enable32Bit;
                appPool.ManagedPipelineMode = context.AppPoolMode == AppPoolMode.Integrated
                                                  ? ManagedPipelineMode.Integrated
                                                  : ManagedPipelineMode.Classic;

                if (!string.IsNullOrEmpty(Config.Instance.AppPoolUsername)) {
                    Logger.Log("Setting AppPool identity...");

                    var password = Config.Instance.AppPoolPassword;

                    if (string.IsNullOrEmpty(password)) {
                        Logger.Warn("Please fill in password for AppPool identity user!");
                        Console.WriteLine("\tUsername: "******"\tPassword: "******"Created a new AppPool with .NET {0} named {1}", appPool.ManagedRuntimeVersion, name);

                // Create site with appool.
                Site site = manager.Sites.Add(name, webProjectDirectory, 80);
                site.ServerAutoStart = true;
                site.Applications[0].ApplicationPoolName = name;
                Logger.Log("Created a new site named " + name);

                site.Bindings.Clear();
                site.Bindings.Add("*:80:" + host, "http");
                Logger.Log("Added binding for " + host);

                try {
                    manager.CommitChanges();
                    Logger.Success("Site and AppPool has been created in IIS.");
                }
                catch (Exception ex) {
                    Logger.Error(ex.ToString());
                    context.ExitAtNextCheck = true;
                }
            }
        }
Пример #3
0
        private static void InitiateInstallTask(WebsiteContext context)
        {
            ContextResolver.ResolveContextDetails(context);

            if (context.ExitAtNextCheck)
                return;

            Console.WriteLine("\nConfiguration:");
            DisplayContext(context);

            if (!context.HasAdministratorPrivileges()) {
                Logger.Space();
                Logger.Warn("WIA needs to run with administrator privileges to modify IIS and HOSTS-file.\nOpen new command prompt with \"Run as administrator\" and try again.");
                return;
            }

            if (context.Force) {
                ProcessTasks(context);
            }
            else {
                Console.WriteLine("\nDo you want to continue installation with this configuration? (y/n)");
                var response = Console.ReadLine();

                if (response != null && response.Equals("y", StringComparison.InvariantCultureIgnoreCase)) {
                    ProcessTasks(context);
                }
            }
        }