IIS 7.0中的奇怪行为 - System.DirectoryServices

问题描述 投票:0回答:3

我在IIS 7.0中面临一个奇怪的问题:

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

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


0
投票

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

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.