2010-05-24 89 views
3

在我的应用程序需要停止应用程序池programmitically IIS 7中我创建了一个本地acccount并给他系统管理员权限。这里是代码停止应用程序池问题Programitically IIS7

Private static void StopApplication() 
{ 
    string serviceHostDeploymentType = "local"; 
       if (serviceHostDeploymentType.Equals("local")) 
       { 
        WindowsIdentityImpersonate newIdentity = new WindowsIdentityImpersonate(); 
        if (newIdentity.Impersonate("AccountName", Environment.MachineName, "Password")) 
        { 
         try 
         { 
          ServerManager serverManager = ServerManager.OpenRemote("Server"); 
          string siteName = GetWebSiteNameById(serverManager, 1); 
          Site parentWebsite = serverManager.Sites[siteName]; 

          if (parentWebsite != null) 
          { 
           ApplicationPool apppool = serverManager.ApplicationPools["Application Pool Name"]; 

           if (apppool != null) 
           { 
            apppool.Stop(); 
           } 
          } 
         } 
         finally 
         { 
          newIdentity.Undo(); 
         } 
        } 
       } 
       else 
       { 
        throw new ConfigurationErrorsException("..."); 
       } 
    } 

我得到一个错误访问被拒绝。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。

Stack Trace 

    at Microsoft.Web.Administration.Interop.IAppHostMethodInstance.Execute() 
     at Microsoft.Web.Administration.ConfigurationElement.ExecuteMethod(String methodName) 
     at Microsoft.Web.Administration.ApplicationPool.Stop() 
     at ServerManagerTesting.Form1.StopApplication() 
     at ServerManagerTesting.Form1.button1_Click(Object sender, EventArgs e) 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at ServerManagerTesting.Program.Main() 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 

回答

0

可能的问题是UAC阻止您控制IIS。看看这个question。所选答案与您自己的问题无关,但其他答案指向一个SDK示例,该示例使用提升的特权启动新的过程。

+0

我也认为它与UAC的问题,但不是添加用户到管理员联系组还有什么做我必须做使它工作 – 2010-05-24 08:57:55

+0

当我运行从“运行方式”的应用程序之外,它不运行。但是这个代码在IIS上的一项服务中。我如何让它运行。从服务中调用时,模拟器不起作用。 – 2010-05-24 12:34:49

+0

请按照引用问题的建议。它显示了如何启用启用所需特权的新流程。 – Timores 2010-05-25 23:16:29