views:

356

answers:

1

I'm trying to write an application that will allow a non-administrator user to create virtual directories in IIS6. I've tried numerous different ways of impersonating a administrative account but nothing seems to work to create the virtual directory.

Here's some of what I've tried:

dim sDirPath as string = "IIS://remotehost/W3svc/1/root"
Dim de As DirectoryEntry = New DirectoryEntry()
de.Path = sDirPath
de.AuthenticationType = AuthenticationTypes.Secure
de.Username = tbxUsername.Text
de.Password = tbxPassword.Text
''Fails here
virtualId = CType(de.Invoke("Create", "IIsWebVirtualDir", dirName), DirectoryEntry)

Using the AccountAlias class from here:

aa.BeginImpersonation()
Try
    dim sDirPath as string = "IIS://remotehost/W3svc/1/root"
    Dim de As DirectoryEntry = New DirectoryEntry()
    de.Path = sDirPath
    de.AuthenticationType = AuthenticationTypes.Secure
    ''Fails Here
    virtualId = CType(de.Invoke("Create", "IIsWebVirtualDir", dirName), DirectoryEntry)
Finally
    aa.EndImpersonation()
End Try

what is the correct way to impersonate another user in winforms application that will allow me to create the virtual directory WITHOUT the user that's running the application being an administrator on the web server? I know this can be done, IIS manager allows you to use "connect as" checkbox to do it.

+2  A: 

It's been a long time since I hit this problem so I may be remembering a few things wrong. But IIRC ...

The root directory of IIS is protected by an ACL that requires administrative privs in order to create a new directory. A simple impersonation likely will not fix this issue because I believe under the hood you are combining both COM and either RPC or Local RPC. Impersonation across COM boundaries is tricky in native code and very tricky from managed code. It's very likley that you are running into an issue in this area with your impersonation trick.

It is possible though to lift the restriction on the web server and have normal users create root directories in IIS. I outlined the process to do so in the following blog post

JaredPar