如何使用System.DirectoryServices访问不同域上的Web服务器

时间:2011-09-19 22:43:06

标签: c# iis-6 directoryservices

我正在尝试编写一个简单的程序来列出IIS服务器的虚拟目录,该服务器位于与本地计算机不同的域中。在创建根DirectoryEntry对象时,我尝试使用域限定符传递凭据,如下所示:

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

然而,我得到了“拒绝访问”的例外情况。这是正确的方法吗?我发现的所有代码示例只能访问本地Web服务器。 我在本地运行WinXP SP3,并尝试连接到运行IIS 6.0版的Win2003 R2(64位)服务器。

1 个答案:

答案 0 :(得分: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();
        }
    }
}