【问题标题】:Replace Current Windows User Running the EXE with another user将当前运行 EXE 的 Windows 用户替换为另一个用户
【发布时间】:2011-08-08 15:16:45
【问题描述】:

假设我构建了一个从网络文件夹读取文件的 Windows 应用程序。网络折叠将访问限制为仅一个用户“fooUser”。该应用程序安装在网络上的多台机器上。

我需要将当前用户替换为“fooUser”,以便能够通过代码访问网络文件夹中的文件。

【问题讨论】:

    标签: c# .net permissions windows


    【解决方案1】:

    这是一个非常简单的模拟方案,可让您在一段时间内成为任何人(前提是您拥有适当的凭据。)
    本课程将为您完成所有繁重的工作......

      public class Impersonator : IDisposable
      {
    
        const int LOGON32_PROVIDER_DEFAULT = 0;
        const int LOGON32_LOGON_INTERACTIVE = 2;
    
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);
    
        private IntPtr token = IntPtr.Zero;
        private WindowsImpersonationContext impersonated;
        private readonly string _ErrMsg = "";
    
        public bool IsImpersonating
        {
          get { return (token != IntPtr.Zero) && (impersonated != null); }
        }
    
        public string ErrMsg
        {
          get { return _ErrMsg; }
        }
    
        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public Impersonator(string userName, string password, string domain)
        {
          StopImpersonating();
    
          bool loggedOn = LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
          if (!loggedOn)
          {
            _ErrMsg = new System.ComponentModel.Win32Exception().Message;
            return;
          }
    
          WindowsIdentity identity = new WindowsIdentity(token);
          impersonated = identity.Impersonate();
        }
    
        private void StopImpersonating()
        {
          if (impersonated != null)
          {
            impersonated.Undo();
            impersonated = null;
          }
    
          if (token != IntPtr.Zero)
          {
            CloseHandle(token);
            token = IntPtr.Zero;
          }
        }
    
        public void Dispose()
        {
          StopImpersonating();
        }
      }
    

    你可以这样使用它;

    using (Impersonator = new Impersonator(yourName,yourPassword,yourDomain))
    {
     // Read files from network drives.
     // Other activities....
    }
    

    将impersonator放在'using'块中非常重要,或者在完成模拟任务后将其释放,否则系统将无限期地继续模拟,这将导致各种问题。

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        你可以看看这个问题LogonUser and delegation是否对你有帮助。

        【讨论】:

          【解决方案4】:

          您可以将映射驱动器设置为使用“fooUser”凭据的文件夹共享。

          但如果您有用户的登录名/密码,您可以让您的代码使用模拟。 根据我对Windows Impersonation from C# 的回答:

          有关模拟代码,请参阅 以下两篇代码项目文章:

          http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

          http://www.codeproject.com/KB/cs/zetaimpersonator.aspx

          以及它们是 Microsoft 知识库文章 基于:

          http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-02-08
            • 1970-01-01
            • 1970-01-01
            • 2011-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多