我想在我的 winforms 应用程序中将 IUSR 帐户添加到该管理员组。下面的代码失败,因为它找不到“NT AUTHORITY\IUSR”的用户:
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry administrators = AD.Children.Find("Administrators", "group");
DirectoryEntry iusr = AD.Children.Find(@"NT AUTHORITY\IUSR", "user");
administrators.Invoke("Add", new object[] {iusr.Path.ToString()});
我意识到这是个坏主意。我这样做是因为我正在编写一个 winforms 应用程序,它以编程方式在 IIS7 中创建一个新网站,仅用于开发目的。该网站已成功创建,但是每当我尝试加载页面时,ASP.NET 都会显示错误“访问被拒绝”。当我将 IUSR 添加到管理员组时,一切正常。我还能尝试什么?下面是我用来创建新网站的代码:
Site site = servermgr.Sites.Add(websitename, physicalpath, port);
Binding binding = site.Bindings.CreateElement();
string bindinginfo = "*:" + port.ToString() + ":" + hostipaddress;
binding.Protocol = "http";
binding.BindingInformation = bindinginfo;
site.Bindings.Clear();
site.Bindings.Add(binding);
site.Applications.Add(applicationpath, applicationphysicalpath);
site.ApplicationDefaults.ApplicationPoolName = "Default";
servermgr.CommitChanges();