[英]How to Use System.DirectoryServices to Access a Web Server on a Different Domain
我正在嘗試編寫一個簡單的程序來列出IIS服務器的虛擬目錄,該服務器與本地計算機不在同一個域中。 創建根DirectoryEntry對象時,我嘗試使用域限定符傳遞憑據,如下所示:
DirectoryEntry entry = new DirectoryEntry("IIS://myremoteserver/W3SVC/1/Root", "mydomain\\myusername", "mypassword");
但是,出現“訪問被拒絕”異常。 這是正確的方法嗎? 我發現的所有代碼示例僅訪問本地Web服務器。 我在本地運行WinXP SP3,並嘗試連接到運行IIS 6.0版的Win2003 R2(64位)服務器。
我決定改用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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.