ADSI C# IIS remote app pool creation

786 Views Asked by At

I have the following problem. I'm trying to create an application pool in the IIS on remote machine. This machine runs MS Windows Server 2012 with IIS 7.5 and has a user "LocalAdmin" which is a local account and administartor. I turned off UAC to prevent Run As Administrator issues. So, now I can:

  1. connect to IIS
  2. create virtual directories
  3. also I use Impersonalisation and can create a directory on the remote machine
  4. access app pools. More details:

    var service = new DirectoryEntry(string.Format(@"IIS://{0}/W3SVC/AppPools", settings.Server), settings.FullUserName, settings.Password);
    DirectoryEntry pool = service.Children.Cast<DirectoryEntry>().FirstOrDefault(x => x.Name == originalAppPoolName);
    

    This will return an existing application pool. If I call pool.CommitChanges() everything's fine. Then I try to create a new application pool

    DirectoryEntry appPool = service.Children.Add(newAppPoolName, "IIsApplicationPool");
    appPool.CommitChanges()
    

    But I receive UnauthorizedAccessException: Access is denied.

How to solve the issue with this exception? Additionally, if I run the same code locally I have no problems: application pool is created and run. So, I belive the problem is with security and authorization. What I need to do to allow my code, that is run on my local machine, to add app pool on the remote server using "LocalAdmin" administrator credentials?

P.S. Also I see in the SecurityLog on the remote server attepts to login with my local credentials. Why does it happen? If I use Impersonalisation nothing changes...

P.P.S. Solved the problem by adding a user to the remote server with the same credentials as on local machine. Is there another way?

0

There are 0 best solutions below