C# 如何使用System.DirectoryServices访问不同域上的Web服务器
我正在尝试编写一个简单的程序来列出IIS服务器的虚拟目录,该服务器位于与本地计算机不同的域中。创建根DirectoryEntry对象时,我尝试使用域限定符传入凭据,如下所示:C# 如何使用System.DirectoryServices访问不同域上的Web服务器,c#,iis-6,directoryservices,C#,Iis 6,Directoryservices,我正在尝试编写一个简单的程序来列出IIS服务器的虚拟目录,该服务器位于与本地计算机不同的域中。创建根DirectoryEntry对象时,我尝试使用域限定符传入凭据,如下所示: DirectoryEntry entry = new DirectoryEntry("IIS://myremoteserver/W3SVC/1/Root", "mydomain\\myusername", "mypassword"); 然而,我得到了一个“访问被拒绝”的异常。这样做对吗?我发现的所有代码示例都只能访问本地
DirectoryEntry entry = new DirectoryEntry("IIS://myremoteserver/W3SVC/1/Root", "mydomain\\myusername", "mypassword");
然而,我得到了一个“访问被拒绝”的异常。这样做对吗?我发现的所有代码示例都只能访问本地web服务器。
我在本地运行WinXP SP3,并尝试连接到运行IIS 6.0版的Win2003 R2(64位)服务器。我决定使用类来完成此操作,这在登录中使用域限定符时有效:
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();
}
}
}