【问题标题】:connection one time to network Drive when Creating files with Impersonator使用 Impersonator 创建文件时连接一次网络驱动器
【发布时间】:2013-12-31 10:17:39
【问题描述】:

我必须在指定用户的网络驱动器中创建许多文件。

我使用this answer 连接不同的用户 我使用 Impersonator 类:

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 impersonator = new Impersonator("UserName", "UserPwd", "UserDomaine"))
{
    if (!Directory.Exists("Z:\\")) // check if Network drive exist
    {
       NetworkDrive drive = new NetworkDrive
       {
           ShareName = @"\\IP\Partage",
           LocalDrive = "Z",
           Force = true
       };

       drive.MapDrive(@"UserDomaine\UserName", "UserPwd");

     }
     File.Create(@"Z:\Log\FileName.txt");
}

但在这种情况下,我发现每次我必须创建文件或更新文件时,代码都会映射驱动器!我在这个函数上做了很多工作。

有办法不每次都映射吗?

我尝试在打开应用程序时将驱动程序与该用户映射,但同样的问题。

【问题讨论】:

    标签: c# impersonation network-drive


    【解决方案1】:

    我认为您不需要映射驱动器。模拟后,您可以直接使用网络驱动器创建文件,它将以模拟用户的身份创建文件。

    using (Impersonator impersonator = new Impersonator("UserName", "UserPwd", "UserDomaine"))
    {
        File.Create(@"\\IP\Partage\Log\FileName.txt");
    }
    

    【讨论】:

    • 从服务器上,您可以尝试从作为用户之一登录的 Windows 资源管理器访问路径吗?
    • 我在本地测试它可以工作(我必须在项目中实现这个并将它发送到服务器生产并告诉你结果
    【解决方案2】:

    尽量不要使用 Using 块。将 Impersonator 声明为全局静态变量。

    【讨论】:

    • 不使用不会使用新用户
    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 2020-05-09
    • 2016-12-11
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多