2011-09-19 44 views
0

我正在嘗試編寫一個簡單程序來列出IIS服務器的虛擬目錄,該服務器位於與本地計算機不同的域上。當創建根DirectoryEntry對象,我試圖在憑據傳遞與域預選賽中,像這樣:如何使用System.DirectoryServices訪問不同域上的Web服務器

DirectoryEntry entry = new DirectoryEntry("IIS://myremoteserver/W3SVC/1/Root", "mydomain\\myusername", "mypassword"); 

我得到一個「訪問被拒絕」異常,但是。這是正確的方法嗎?我找到的所有代碼示例都只能訪問本地Web服務器。 我在本地運行WinXP SP3,並試圖連接到運行IIS 6.0的Win2003 R2(64位)服務器。

回答

0

我決定用System.Management類來完成此相反,當我在登錄使用域限定其工作原理:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Management; 

namespace MyProgram 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      ConnectionOptions options = new ConnectionOptions(); 
      options.Authentication = AuthenticationLevel.PacketPrivacy; 
      options.Username = "somedomain\\username"; 
      options.Password = "password"; 
      ManagementPath path = new ManagementPath(); 
      path.Server = "someserver"; 
      path.NamespacePath = "root/MicrosoftIISv2"; 
      ManagementScope scope = new ManagementScope(path, options); 

      string Query = "select * from IIsWebVirtualDirSetting"; 
      using (ManagementObjectSearcher search = new ManagementObjectSearcher(scope, new ObjectQuery(Query))) 
      { 
       ManagementObjectCollection results = search.Get(); 
       foreach (ManagementObject obj in results) 
       { 
        Console.WriteLine(obj.Properties["Name"].Value); 
       }     
      }   
      Console.ReadLine(); 
     } 
    } 
} 
相關問題