Beispiel #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string AnonymousUserName = "******" + Regex.Replace(ServerComment.Text, @"\.|-", "_");
            string AnonymousUserPass = Comm.Random(20);

            if (AnonymousUserName.Length > 20)
            {
                AnonymousUserName = AnonymousUserName.Substring(0, 20);
            }
            WinNT.User.Del(AnonymousUserName);
            WinNT.User.Add(AnonymousUserName, AnonymousUserPass);


            if (!Directory.Exists(Path.Text))
            {
                Directory.CreateDirectory(Path.Text);
                File.WriteAllText(Path.Text + "\\CrazyIIS.html", "hello html");
                File.WriteAllText(Path.Text + "\\CrazyIIS.asp", "<%=1+1%>..OK");
                File.WriteAllText(Path.Text + "\\CrazyIIS.aspx", "<%=1+1%>..OK");
            }


            DirectoryEntry dir = new DirectoryEntry("IIS://localhost/w3svc");

            List <Int32> AllWebSiteId = Comm.GetAllWebSiteID();
            string       NewWebSiteId = Comm.GetNewWebSiteID(AllWebSiteId);

            AllWebSiteId.Add(Convert.ToInt32(NewWebSiteId));

            DirectoryEntry web = dir.Children.Add(NewWebSiteId, "IIsWebServer");

            web.Properties["AuthFlags"].Value        = 0;
            web.Properties["ServerAutoStart"].Value  = true;
            web.Properties["ServerBindings"].Value   = ServerBindings.Text.Split(',');
            web.Properties["ServerComment"].Value    = ServerComment.Text;
            web.Properties["LogFileDirectory"].Value = LogFileDirectory.Text;

            web.CommitChanges();

            DirectoryEntry root = web.Children.Add("root", "IIsWebVirtualDir");

            root.Properties["Path"].Value = Path.Text;

            //root.Invoke("AppCreate", true);//创建应用程序

            root.Properties["EnableDirBrowsing"][0] = false;
            root.Properties["AccessExecute"][0]     = false;
            root.Properties["AccessWrite"][0]       = false;
            root.Properties["AccessRead"][0]        = true; //设置读取权限
            root.Properties["AccessScript"][0]      = true; //执行权限
            root.Properties["AuthFlags"].Value      = 1;    //0表示不允许匿名访问,1表示就可以3为基本身份验证,7为windows继承身份验证


            root.Properties["AnonymousUserName"].Value = AnonymousUserName;
            root.Properties["AnonymousUserPass"].Value = AnonymousUserPass;
            root.Properties["AppFriendlyName"].Value   = ServerComment.Text;
            root.Properties["AppPoolId"].Value         = AppPoolId.Text;
            root.Properties["AppIsolated"].Value       = 2;
            root.Properties["AppRoot"].Value           = string.Format("/LM/W3SVC/{0}/Root", NewWebSiteId);

            root.CommitChanges();

            if (hosts.Checked)
            {
                Comm.Write2hosts(("127.0.0.1," + ServerComment.Text + ",127.0.0.1,www." + ServerComment.Text).Split(','), false);
            }

            NTFS.ACL.Add(Path.Text, AnonymousUserName, FileSystemRights.FullControl);
            NTFS.ACL.Add(Path.Text, "NETWORK SERVICE", FileSystemRights.FullControl);
            MessageBox.Show("OK");
        }