2012-01-13 33 views
1

我想在我的winforms應用程序中將IUSR帳戶添加到該管理員組。下面的代碼失敗,因爲它無法找到「NT AUTHORITY \ 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(); 
+5

你爲什麼這樣做? – 2012-01-13 21:04:31

+1

你不能添加一個不存在的用戶......你的問題到底是什麼? – Yahia 2012-01-13 21:06:34

+3

這是一個***糟糕的主意 – 2012-01-13 21:09:56

回答

1

正確的解決辦法是要求身份驗證的頁面。然後這會提示瀏覽器讓你登錄;如果您使用適當授權的帳戶,該頁面將可以創建新網站。

IIS7允許您通過(例如)ApplicationHost.config配置此功能,而以前的版本需要您編輯元數據庫(通常來自IIS管理器)。

編輯:根據身份驗證,瀏覽器和網絡配置,可以安排瀏覽器使用您的域帳戶自動登錄頁面。

1
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
DirectoryEntry administrators = AD.Children.Find("Administrators", "group"); 

administrators.Invoke("Add", new object[] {"WinNT://NT AUTHORITY/IUSR"}); 
相關問題