System.Runtime.InteropServices.COMException trying to manage remote iis7 pools

0

I'm writing a tool to automate the deploying of several website to remote machines, and i need to manage some IIS7 pools during the website publishing.

I wrote a function to manage local pools without impersonation and it worked well, so i edited like this:

[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

public static void ManagePool(string command, string appPool)
{
    IntPtr admin_token = default(IntPtr);
    WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
    WindowsIdentity wid_admin = null;
    WindowsImpersonationContext wic = null;
    try
    {
        if (LogonUser("username", "domain", "password, 9, 0, ref admin_token) != 0)
        {
            wid_admin = new WindowsIdentity(admin_token);
            wic = wid_admin.Impersonate();

            using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPool))
            {
                appPoolEntry.Invoke(command, null);
                appPoolEntry.Close();
            }
        }
    }
    catch (System.Exception se)
    {
        int ret = Marshal.GetLastWin32Error();
        log.append("Error code: " + ret.ToString());
        log.append(se.Message);
    }
    finally
    {
        if (wic != null)
        {
            wic.Undo();
        }
    }
}

but when i try to call ManagePool("Stop", "IIS://172.23.231.199/W3SVC/AppPools/My Pool") i get this error:

Unknown error (0x80005000) 
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_NativeObject()
   at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

How to manage them remotely? is there any way to get it works?

c#
iis-7
impersonation
asked on Stack Overflow Jun 30, 2014 by Doc

1 Answer

0

I resolved with:

IntPtr admin_token = default(IntPtr);
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin = null;
WindowsImpersonationContext wic = null;
try
{
    if (LogonUser("username", "domain", "password", 9, 0, ref admin_token) != 0)
    {
        wid_admin = new WindowsIdentity(admin_token);
        wic = wid_admin.Impersonate();
        ServerManager.OpenRemote("123.123.123.123").ApplicationPools["My Pool"].Start();
    }
}

thanks all for the almost-overwhelming flow of replys

:P

answered on Stack Overflow Jul 14, 2014 by Doc

User contributions licensed under CC BY-SA 3.0