0

我在 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 中对虚拟目录中的文件夹/文件进行一些手动操作,否则它似乎不会在活动目录中创建相应的元数据?

4

3 回答 3

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 没有帮助

于 2009-01-27T06:23:23.077 回答
0

如果您在第三行之后立即调用 RefreshCache() 会有所帮助吗?

DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password");
dir.AuthenticationType = AuthenticationTypes.Secure;
dir.RefreshCache();
于 2009-01-22T16:08:48.413 回答
0

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

于 2009-01-22T17:04:11.000 回答