如何临时模拟用户打开文件?

2023-12-07

我想暂时模拟域用户帐户以从 ASP.NET 站点读取网络驱动器上的文件。

我不想为整个站点设置模拟或在服务器上设置映射驱动器。


我最终使用了代码Michiel van Otegem:WindowsImpersonationContext 变得简单并添加了 IDisposable 的实现。我发现这个在关于 ASP.NET 中模拟的另一个问题.

Usage:

using (WindowsImpersonationContextFacade impersonationContext
     = new WindowsImpersonationContextFacade(
                    Settings.Default.ImpersonationDomain, 
                    Settings.Default.ImpersonationUser,
                    Settings.Default.ImpersonationPass))
                {
                    transactions  = TransactionLoader.Load(filePath);
                }

Code:

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.ComponentModel;

namespace MyNamespace
{
    public class WindowsImpersonationContextFacade : IDisposable
    {
        [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 static extern bool CloseHandle(IntPtr handle);

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

        private string m_Domain;
        private string m_Password;
        private string m_Username;
        private IntPtr m_Token;

        private WindowsImpersonationContext m_Context = null;

        protected bool IsInContext
        {
            get { return m_Context != null; }
        }

        public WindowsImpersonationContextFacade(string domain, string username, string password)
        {
            m_Domain = domain;
            m_Username = username;
            m_Password = password;
            Enter();
        }

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        private void Enter()
        {
            if (this.IsInContext) return;
            m_Token = IntPtr.Zero;
            bool logonSuccessfull = LogonUser(
                m_Username,
                m_Domain,
                m_Password,
                LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT,
                ref m_Token);
            if (logonSuccessfull == false)
            {
                int error = Marshal.GetLastWin32Error();
                throw new Win32Exception(error);
            }
            WindowsIdentity identity = new WindowsIdentity(m_Token);
            m_Context = identity.Impersonate();
        }

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        private void Leave()
        {
            if (this.IsInContext == false) return;
            m_Context.Undo();

            if (m_Token != IntPtr.Zero) CloseHandle(m_Token);
            m_Context = null;
        }

        public void Dispose()
        {
            Leave();
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何临时模拟用户打开文件? 的相关文章

随机推荐