Unkown name exception while creating virtual directory with DirectoryServices

1

I'm trying to create a virtual directory on a remote server using DirectoryServices by cloning the settings of another virtual directory, but I get a COMException: Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) when invoking AppCreate.

This is the code I've written (simplified with most of tests removed):

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;
  }
}

I tried some variations (see comments in the code) using AppCreate2 instead of AppCreate or calling a CommitChanges and/or invoking SetInfo before the AppCreate, but all lead to the same exception.

The user credentials I'm using has adminstrator rights on the remote server. In fact if I copy the program to the remote server and run it there with the same credentials (I logged in with the same user on the remote server), using 'localhost' as servername and the user variable = null, it works without throwing the exception, so it must have something to do with the remote execution.

The remote server is a Windows Server 2003 R2 SP2 (32bit), the machine running this program is a Windows 7 Professional (64 bit).

Does anyone have any ideas ?

c#
iis
directoryservices
asked on Stack Overflow Mar 19, 2012 by Marc • edited Mar 20, 2012 by Marc

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0