使用目录服务创建虚拟目录时出现未知名称异常

时间:2012-03-19 12:24:52

标签: c# iis directoryservices

我正在尝试使用DirectoryServices通过克隆另一个虚拟目录的设置在远程服务器上创建虚拟目录,但我得到 COMException :未知名称。 (调用 AppCreate 时,来自HRESULT的异常:0x80020006(DISP_E_UNKNOWNNAME))。

这是我编写的代码(已删除大部分测试简化):

public bool CreateVirtualDirectory(string serverName, string primaryVirtualDirectoryName, string virtualDirectoryName, MyUser user)
{
  try
  {
    DirectoryEntry directoryEntry = null;
    if (user != null)
      directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root", user.UserName, user.Password, AuthenticationTypes.Secure | AuthenticationTypes.Sealing);
    else
      directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root");

    DirectoryEntry primaryVirtualDirectory = directoryEntry.Children.Find(primaryVirtualDirectoryName, directoryEntry.SchemaClassName);

    DirectoryEntry virtualDirectory = directoryEntry.Children.Add(virtualDirectoryName, directoryEntry.SchemaClassName);
    virtualDirectory.CommitChanges();

    virtualDirectory.Properties["Path"].Value = primaryVirtualDirectory.Properties["Path"].Value;
    virtualDirectory.Properties["AppFriendlyName"][0] = virtualDirectoryName;
    if (primaryVirtualDirectory.Properties["UNCUserName"] != null && primaryVirtualDirectory.Properties["UNCUserName"].Value != null && primaryVirtualDirectory.Properties["UNCUserName"].Value.ToString().Length > 0)
    {
      // IIS6 of IIS7
      virtualDirectory.Properties["AuthNTLM"][0] = true;
      virtualDirectory.Properties["UNCUserName"].Value = primaryVirtualDirectory.Properties["UNCUserName"].Value;
      virtualDirectory.Properties["UNCPassword"].Value = primaryVirtualDirectory.Properties["UNCPassword"].Value;
    }
    else
    {
      // Older versions
      virtualDirectory.Properties["AuthFlags"][0] = 5; // MD_AUTH_ANONYMOUS | MD_AUTH_NT
      virtualDirectory.Properties["AnonymousUserName"].Value = primaryVirtualDirectory.Properties["AnonymousUserName"].Value;
      virtualDirectory.Properties["AnonymousUserPass"].Value = primaryVirtualDirectory.Properties["AnonymousUserPass"].Value;
    }
    virtualDirectory.Properties["AccessRead"][0] = true;
    virtualDirectory.Properties["AccessExecute"][0] = true;
    virtualDirectory.Properties["AccessWrite"][0] = false;
    virtualDirectory.Properties["AccessScript"][0] = true;
    virtualDirectory.Properties["EnableDefaultDoc"][0] = true;
    virtualDirectory.Properties["EnableDirBrowsing"][0] = false;
    if (primaryVirtualDirectory.Properties["AppPoolId"] != null && primaryVirtualDirectory.Properties["AppPoolId"].Value != null && primaryVirtualDirectory.Properties["AppPoolId"].Value.ToString().Length > 0)
      virtualDirectory.Properties["AppPoolId"].Value = primaryVirtualDirectory.Properties["AppPoolId"].Value;
    virtualDirectory.Properties["ScriptMaps"].Value = primaryVirtualDirectory.Properties["ScriptMaps"].Value;

    // I Tried adding these 3 lines but I keeo getting the same exception
    // virtualDirectory.CommitChanges();
    // virtualDirectory.Invoke("SetInfo");
    // virtualDirectory.CommitChanges();

    virtualDirectory.Invoke("AppCreate", true);
    // I tried this instead but I still get the same exception
    // virtualDirectory.Invoke("AppCreate2", 0);
    virtualDirectory.CommitChanges();

    return true;
  }
  catch (Exception exception)
  {
    return false;
  }
}

我在AppCreate之前使用AppCreate2而不是AppCreate或调用CommitChanges和/或调用SetInfo尝试了一些变体(参见代码中的注释),但都导致了同样的异常。

我正在使用的用户凭据在远程服务器上具有管理员权限。事实上,如果我将程序复制到远程服务器并使用相同的凭据(我使用远程服务器上的同一用户登录)运行它,使用“localhost”作为servername并且用户变量= null,它可以正常工作而不会丢弃例外,所以它必须与远程执行有关。

远程服务器是Windows Server 2003 R2 SP2(32位),运行此程序的计算机是Windows 7 Professional(64位)。

有没有人有任何想法?

0 个答案:

没有答案