模拟和BackgroundWorker

发布于 2024-07-16 09:28:26 字数 2914 浏览 7 评论 0原文

当尝试使用 BackgroundWorker 类进行模拟时,我遇到了一些问题。 根据 Google 的答案,我得到了这段代码来模拟

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(对象发送者, DoWorkEventArgs e) {         
              MyImpersonation myImpersonation = new MyImpersonation(); 
              myImpersonation.impersonateValidUser(用户,域,通行证)         
             //运行一些代码...         
             myImpersonation.undoImpersonation();         
      } 
      
  • 代码通过

    启动
    BGWorker.RunWorkerAsync(); 
      

如我之前所说,不会引发任何错误,只是代码的行为就像我没有运行任何模拟一样,即使用其默认凭据。

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

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

I have a little bit of a problem when trying to use the BackgroundWorker class with impersonation. Following the answers from Google, I got this code to impersonate

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;
    }
}

It worked really well until I used it with the BackgroundWorker class. In this case, I've added a impersonation in the the code that runs asynchronously. I have no errors, but the issue I'm having is that the impersonation does not work when it is used in the async method.

In code this looks something like this:

  • instantiate a BGWorker, and add an event handler to the DoWork event:

    _bgWorker = new BackgroundWorker();
    _bgWorker.DoWork += new DoWorkEventHandler(_bgWorker_DoWork);
    
  • in the above handler, a impersonation is made before running some code.

    private void _bgWorker_DoWork(object sender, DoWorkEventArgs e) {        
            MyImpersonation myImpersonation = new MyImpersonation();
            myImpersonation.impersonateValidUser(user, domain, pass)        
           //run some code...        
           myImpersonation.undoImpersonation();        
    }
    
  • the code is launched with

    BGWorker.RunWorkerAsync();
    

As I said before, no error is thrown, only that the code acts as if I didn't run any impersonation, that is with its default credentials.

Moreover, the impersonation method returns true, so the impersonation took place at a certain level, but probably not on the current thread.

This must happen because the async code runs on another thread, so there must be something that needs to be added to the MyImpersonation class. But what? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-07-23 09:28:26

我也有同样的问题。 我有一个后台工作人员在 DoWork 事件中模拟静态类。 我正在尝试模拟另一个活动域用户,以便读取主用户无权访问的目录。 如果我在 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

I'm having this same problem. I have a background worker that does impersonation with a static class in the DoWork event. I'm attempting to impersonate another Active Domain user in order to read a directory that the main user does not have access to. If I am running the application in the IDE, the impersonated user can read the files with no problem. If I run the application from the file system, the impersonation still happens, but an exception is thrown saying the impersonated user does not have access.

From what I've read it's due to setting Assembly:SecurityPermissionAttribute and Assembly:PermissionSetAttribute.

Before the class declaration I have the following:

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

Before my StartImpersonating() function, I have:

<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _

I'm working with VB.NET and not C#, however.

-D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文