[英]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.