C# 模拟和后台工作人员

C# 模拟和后台工作人员,c#,.net,backgroundworker,impersonation,C#,.net,Backgroundworker,Impersonation,我在尝试将BackgroundWorker类与模拟一起使用时遇到了一点问题。根据谷歌的答案,我得到了这个代码来模拟 public class MyImpersonation { WindowsImpersonationContext impersonationContext; [DllImport("advapi32.dll")] public static extern int LogonUserA(String lpszUserName, Stri

我在尝试将
BackgroundWorker
类与模拟一起使用时遇到了一点问题。根据谷歌的答案,我得到了这个代码来模拟

public class MyImpersonation {

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public 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)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

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

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

    public bool impersonateValidUser(String userName, String domain, String password) {
        WindowsIdentity tempWindowsIdentity;
        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) {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null) {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }
}
在我将它与
BackgroundWorker
类一起使用之前,它工作得非常好。在本例中,我在异步运行的代码中添加了一个模拟。我没有错误,但我遇到的问题是,在异步方法中使用模拟时,模拟不起作用

在代码中,这类似于:

  • 实例化BGWorker,并将事件处理程序添加到DoWork事件:

    _bgWorker = new BackgroundWorker();
    _bgWorker.DoWork += new DoWorkEventHandler(_bgWorker_DoWork);
    
  • 在上面的处理程序中,在运行某些代码之前进行模拟

    private void _bgWorker_DoWork(object sender, DoWorkEventArgs e) {        
            MyImpersonation myImpersonation = new MyImpersonation();
            myImpersonation.impersonateValidUser(user, domain, pass)        
           //run some code...        
           myImpersonation.undoImpersonation();        
    }
    
  • 该代码是用

    BGWorker.RunWorkerAsync();
    
正如我前面所说,不会抛出错误,只是代码的行为就像我没有运行任何模拟一样,即使用其默认凭据

此外,模拟方法返回true,因此模拟发生在某个级别,但可能不在当前线程上


这必须发生,因为异步代码在另一个线程上运行,因此必须向
MyImpersonation
类添加一些内容。但是什么?:)

我也有同样的问题。我有一个后台工作人员,在DoWork事件中模拟静态类。我试图模拟另一个Active Domain用户,以便读取主用户无权访问的目录。如果我在IDE中运行应用程序,模拟用户可以毫无问题地读取文件。如果我从文件系统运行应用程序,模拟仍然会发生,但会抛出一个异常,说明被模拟的用户没有访问权限

据我所知,这是由于设置了Assembly:SecurityPermissionAttribute和Assembly:PermissionSetAttribute

在类声明之前,我有以下内容:

    <Assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode:=True), _
Assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name:="FullTrust")> 

在开始使用StartImpersonating()函数之前,我有:

<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
_
不过,我使用的是VB.NET而不是C#


-我也有同样的问题。我有一个后台工作人员,在DoWork事件中模拟静态类。我试图模拟另一个Active Domain用户,以便读取主用户无权访问的目录。如果我在IDE中运行应用程序,模拟用户可以毫无问题地读取文件。如果我从文件系统运行应用程序,模拟仍然会发生,但会抛出一个异常,说明被模拟的用户没有访问权限

据我所知,这是由于设置了Assembly:SecurityPermissionAttribute和Assembly:PermissionSetAttribute

在类声明之前,我有以下内容:

    <Assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode:=True), _
Assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name:="FullTrust")> 

在开始使用StartImpersonating()函数之前,我有:

<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
_
不过,我使用的是VB.NET而不是C#


-D

如果您提到在后台工作人员类中遇到的错误种类,可能会有所帮助。@Lucian D:我同意Gwlosa的观点,如果该类有效,很好,但是如果出现错误或不起作用,您需要指定如何操作。抱歉,伙计们,我已经更正了我的帖子。我希望现在更清楚:)如果你提到你在后台工作人员类中遇到了什么样的错误,可能会有所帮助。@Lucian D:我同意Gwlosa,如果这个类工作,很好,但是如果有错误或不工作,你需要指定如何操作。抱歉,伙计们,我已经更正了我的帖子。我希望现在更清楚:)