Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WMI和Win32_进程创建远程进程_C#_Asp.net_Wmi - Fatal编程技术网

C# 使用WMI和Win32_进程创建远程进程

C# 使用WMI和Win32_进程创建远程进程,c#,asp.net,wmi,C#,Asp.net,Wmi,我正试图从一个网站在一台远程机器上启动一个进程。当用户按下网站上的特定按钮时,我需要在远程机器上启动PowerShell 我正在使用C#中的System.Management连接到远程计算机并创建Win32#U进程对象。当我在VisualStudio中以自己的域帐户启动网站并单击按钮时,这一切在两个VM之间都可以正常工作。我在两个虚拟机上都是admin组的成员,我可以看到脚本在远程虚拟机上尽职尽责地运行 我已将网站部署到系统测试环境中,该网站现在运行的服务帐户不是web服务器或我的测试Power

我正试图从一个网站在一台远程机器上启动一个进程。当用户按下网站上的特定按钮时,我需要在远程机器上启动PowerShell

我正在使用C#中的System.Management连接到远程计算机并创建Win32#U进程对象。当我在VisualStudio中以自己的域帐户启动网站并单击按钮时,这一切在两个VM之间都可以正常工作。我在两个虚拟机上都是admin组的成员,我可以看到脚本在远程虚拟机上尽职尽责地运行

我已将网站部署到系统测试环境中,该网站现在运行的服务帐户不是web服务器或我的测试PowerShell脚本所在的远程服务器上的管理员

我已向运行与网站关联的应用程序池的帐户授予远程VM上的以下权限: -WMI和所有子命名空间中ROOT\CIMV2的完全权限 -完全DCOM权限

远程VM上没有运行防火墙

我基本上遵循了以下文章:

我还尝试将运行该网站的帐户添加到两个虚拟机上的管理组中,但没有成功。当这不起作用时,我不知道下一步该去哪里

有没有人遇到过这种问题

非常感谢


Chris

尝试使用模拟连接到具有管理员权限的远程计算机。下面是我为编程模拟创建的一个类:

using System;
using System.Security.Principal;
using System.Diagnostics;
using System.Runtime.InteropServices;

/// <summary> 
/// Leverages the Windows API (advapi32.dll) to programmatically impersonate a user. 
/// </summary> 
public class ImpersonationContext : IDisposable 
{ 
    #region constants 

    private const int LOGON32_LOGON_INTERACTIVE = 2; 
    private const int LOGON32_PROVIDER_DEFAULT = 0; 

    #endregion 

    #region global variables 

    private WindowsImpersonationContext impersonationContext; 
    private bool impersonating; 

    #endregion 

    #region unmanaged code 

    [DllImport("advapi32.dll")] 
    private static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); 

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool RevertToSelf(); 

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
    private static extern bool CloseHandle(IntPtr handle); 

    #endregion 

    #region constructors 

    public ImpersonationContext() 
    { 
        impersonating = false; 
    } 

    /// <summary> 
    /// Overloaded constructor and begins impersonating. 
    /// </summary> 
    public ImpersonationContext(string userName, string password, string domain) 
    { 
        this.BeginImpersonationContext(userName, password, domain); 
    } 

    #endregion 

    #region impersonation methods 

    /// <summary> 
    /// Begins the impersonation context for the specified user. 
    /// </summary> 
    /// <remarks>Don't call this method if you used the overloaded constructor.</remarks> 
    public void BeginImpersonationContext(string userName, string password, string domain) 
    { 
        //initialize token and duplicate variables 
        IntPtr token = IntPtr.Zero; 
        IntPtr tokenDuplicate = IntPtr.Zero; 

        if (RevertToSelf()) 
        { 
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) 
            { 
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
                { 
                    using (WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate)) 
                    { 
                        //begin the impersonation context and mark impersonating true 
                        impersonationContext = tempWindowsIdentity.Impersonate(); 
                        impersonating = true; 
                    } 
                } 
            } 
        } 

        //close the handle to the account token 
        if (token != IntPtr.Zero) 
            CloseHandle(token); 

        //close the handle to the duplicated account token 
        if (tokenDuplicate != IntPtr.Zero) 
            CloseHandle(tokenDuplicate); 
    } 

    /// <summary> 
    /// Ends the current impersonation context. 
    /// </summary> 
    public void EndImpersonationContext() 
    { 
        //if the context exists undo it and dispose of the object 
        if (impersonationContext != null) 
        { 
            //end the impersonation context and dispose of the object 
            impersonationContext.Undo(); 
            impersonationContext.Dispose(); 
        } 

        //mark the impersonation flag false 
        impersonating = false; 
    } 

    #endregion 

    #region properties 

    /// <summary> 
    /// Gets a value indicating whether the impersonation is currently active. 
    /// </summary> 
    public bool Impersonating 
    { 
        get 
        { 
            return impersonating; 
        } 
    } 

    #endregion 

    #region IDisposable implementation 

    ~ImpersonationContext() 
    { 
        Dispose(false); 
    } 

    public void Dispose() 
    { 
        Dispose(true);                
    } 

    protected virtual void Dispose(bool disposing) 
    { 
        if (disposing) 
        { 
            if (impersonationContext != null) 
            { 
                impersonationContext.Undo(); 
                impersonationContext.Dispose(); 
            } 
        } 
    } 

    #endregion     
} 
使用系统;
使用System.Security.Principal;
使用系统诊断;
使用System.Runtime.InteropServices;
///  
///利用Windows API(advapi32.dll)以编程方式模拟用户。
///  
公共类模拟上下文:IDisposable
{ 
#区域常数
private const int LOGON32\u LOGON\u INTERACTIVE=2;
private const int LOGON32_PROVIDER_DEFAULT=0;
#端区
#区域全局变量
私有WindowsImpersonationContext impersonationContext;
私人布尔模拟;
#端区
#区域非托管代码
[DllImport(“advapi32.dll”)]
私有静态外部int LogonUserA(字符串lpszUserName、字符串lpszDomain、字符串lpszPassword、int dwLogonType、int dwLogonProvider、ref IntPtr phToken);
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部int DuplicateToken(IntPtr hToken、int impersonationLevel、ref IntPtr hNewToken);
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部bool retertoself();
[DllImport(“kernel32.dll”,CharSet=CharSet.Auto)]
私有静态外部布尔闭合句柄(IntPtr句柄);
#端区
#区域构造函数
公共模拟上下文()
{ 
冒充=假;
} 
///  
///重载构造函数并开始模拟。
///  
公共模拟上下文(字符串用户名、字符串密码、字符串域)
{ 
this.BeginImpersonationContext(用户名、密码、域);
} 
#端区
#区域模拟方法
///  
///开始指定用户的模拟上下文。
///  
///如果使用重载构造函数,请不要调用此方法。
public void BeginImpersonationContext(字符串用户名、字符串密码、字符串域)
{ 
//初始化令牌并复制变量
IntPtr令牌=IntPtr.Zero;
IntPtr-tokenDuplicate=IntPtr.Zero;
if(restortoself())
{ 
if(LogonUserA(用户名、域、密码、LOGON32\u登录\u交互、LOGON32\u提供程序\u默认值、ref令牌)!=0)
{ 
if(DuplicateToken(token,2,ref tokenDuplicate)!=0)
{ 
使用(WindowsIdentity tempWindowsIdentity=新WindowsIdentity(令牌复制))
{ 
//开始模拟上下文并将模拟标记为true
impersonationContext=tempWindowsIdentity.Impersonate();
模仿=真实;
} 
} 
} 
} 
//关闭帐户令牌的句柄
if(令牌!=IntPtr.Zero)
闭合手柄(令牌);
//关闭重复帐户令牌的句柄
如果(令牌重复!=IntPtr.Zero)
闭合手柄(重复);
} 
///  
///结束当前模拟上下文。
///  
public void EndImpersonationContext()
{ 
//如果上下文存在,请撤消它并处置该对象
if(impersonationContext!=null)
{ 
//结束模拟上下文并处置该对象
impersonationContext.Undo();
impersonationContext.Dispose();
} 
//将模拟标志标记为false
冒充=假;
} 
#端区
#区域属性
///  
///获取一个值,该值指示模拟当前是否处于活动状态。
///  
公共场所模仿
{ 
得到
{ 
返回模拟;
} 
} 
#端区
#区域IDisposable实现
~ImpersonationContext()
{ 
处置(虚假);
} 
公共空间处置()
{ 
处置(真实);
} 
受保护的虚拟void Dispose(bool disposing)
{ 
如果(处置)
{ 
if(impersonationContext!=null)
{ 
impersonationContext.Undo();
impersonationContext.Dispose();
} 
} 
} 
#端区
} 

非常感谢您的代码。现在我知道发生了什么,我可能需要它来解决问题

事实证明,问题与Kerberos有关。我发现如果我在本地进入IIS管理器并“浏览”了