【问题标题】:Weird behaviour in IIS 7.0 - System.DirectoryServicesIIS 7.0 中的奇怪行为 - System.DirectoryServices
【发布时间】:2009-01-22 10:49:17
【问题描述】:

我在 IIS 7.0 中遇到了一个奇怪的问题:

我在 IIS 中有以下虚拟目录:

并且在 IIS 中的虚拟目录上只启用了 Windows 身份验证模式

现在,如果我尝试以这种方式为 TestV/Folder/file.aspx 获取关联的 DirectoryEntry:

string vDir = @"/TestV/folder/file.aspx";

            DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password");
            dir.AuthenticationType = AuthenticationTypes.Secure;

            try
            {
                Console.WriteLine(dir.Name);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }

            Console.WriteLine("");

我得到了例外: "系统找不到指定的路径"

现在,如果我返回 IIS,然后执行以下步骤: 右键单击TestV/Folder启用匿名身份验证模式,然后再次禁用它

右击TestV/Folder/file.aspx启用匿名认证模式,然后再次禁用

基本上我只是对 aspx 文件 Testv/Folder/file.aspx 执行了一些手动访问。

经过上述步骤,如果我重新运行程序,代码就可以成功访问目录条目并成功打印名称(file.aspx)

这里有什么问题?

更多信息:

我在 IIS 6.0 上也看到了这种行为。因此,除非我在 IIS 中对虚拟目录中的文件夹/文件进行一些手动操作,否则它似乎不会在活动目录中创建相应的元数据?

【问题讨论】:

    标签: c# iis-7 active-directory


    【解决方案1】:

    我得到了问题的答案(在我的一位同事的帮助下)

    解决方法如下: 1.程序在访问虚拟目录下的文件/文件夹之前,需要在IIS元数据中添加(伪?)条目,然后我们才能访问该条目:

    try
                {
                    // make pseudo entries:
                    DirectoryEntry folder = rootDir.Children.Add("Folder", "IISWebDirectory");
                    folder.CommitChanges();
                    file = folder.Children.Add("File.aspx", "IISWebFile");
                    file.CommitChanges();
                }
    

    然后瞧它的工作原理

    PS:

    DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password");
    dir.AuthenticationType = AuthenticationTypes.Secure;
    dir.RefreshCache();
    

    Directory.Refresh 没有帮助

    【讨论】:

    • 可以使用静态的 DirectoryEntry.Exists 方法查看条目是否存在。
    【解决方案2】:

    在第三行之后调用 RefreshCache() 有帮助吗?

    DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password");
    dir.AuthenticationType = AuthenticationTypes.Secure;
    dir.RefreshCache();
    

    【讨论】:

      【解决方案3】:

      虽然这并不完全是一个答案,但我要指出 System.DirectoryServices 通常不用于与 IIS 交互。虽然它可以让您访问 IIS 设置,但 WMI 通常是更好的选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多