Networking 将文件移动到网络共享(通过模拟)C#

Networking 将文件移动到网络共享(通过模拟)C#,networking,c#-4.0,impersonation,Networking,C# 4.0,Impersonation,我一直在C#(.net4)从事一个项目。Project几乎允许人们将文件从本地计算机上传到网络共享 网络共享是安全的。它只能由在active directory中创建的名为“代理”的用户访问 我做了一些研究,发现了这个我用来模仿的类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using Sys

我一直在C#(.net4)从事一个项目。Project几乎允许人们将文件从本地计算机上传到网络共享

网络共享是安全的。它只能由在active directory中创建的名为“代理”的用户访问

我做了一些研究,发现了这个我用来模仿的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace Datacom.CorporateSys.Utilities
{
    public class ImpersonateUser
    {
        [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);

        WindowsImpersonationContext impersonationContext;

        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        private string p;
        private string p_2;
        private string p_3;


        private String UserName
        {
            set;
            get;
        }

        private String Domain
        {
            set;
            get;
        }

        private String Password
        {
            set;
            get;
        }

        /// <summary>
        /// Impersonates the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="domain">The domain.</param>
        /// <param name="password">The password.</param>
        public ImpersonateUser(string userName, string domain, string password)
        {
            UserName = userName;
            Domain = domain;
            Password = password;
        }

        /// <summary>
        /// Impersonates the valid user.
        /// </summary>
        /// <returns></returns>
        public bool impersonateValidUser()
        {
            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;
        }

        /// <summary>
        /// Undoes the impersonation.
        /// </summary>
        public void undoImpersonation()
        {
            impersonationContext.Undo();
        }
    }
}
而且它有效!我在模拟方面从来没有遇到过问题-从不抛出异常。它工作正常,可以将文件上传到服务器。然而,当我开始进行更多的测试时,我发现如果代理用户没有登录到服务器(通常我打开RDS登录并退出,而不注销)

我遇到不同的异常-网络路径未找到异常,仅当我刚刚重新启动服务器且“代理”未登录时才会发生该异常。

我的第一个想法是模拟类有问题,但是它在工作时模拟的很好(即文件拥有代理用户的所有权)。然后我想可能需要登录“代理”,以便操作系统可以使用其权限实际访问\server\uploads

我现在非常迷茫,不知道如何解决它。 请注意:我无法控制服务器。服务器是win2k8,安装了桌面体验(否则我无法访问任何网络位置)


谢谢

授予代理帐户访问权限“作为批处理作业登录”,并使用
`LOGON32\u LOGON\u batch
代替交互式登录。

授予代理帐户访问权限“作为批处理作业登录”,并使用
`LOGON32\u LOGON\u batch
代替交互式登录。

我做了一些调查。给我的网络路径实际上是通过WebDAV访问的,所以我被建议在WinServer2K8上安装桌面体验。此示例适用于正常的“网络”共享。但是,它无法通过webDAV重定向器(桌面体验)模拟webDAV。我做了一些调查。给我的网络路径实际上是通过WebDAV访问的,所以我被建议在WinServer2K8上安装桌面体验。此示例适用于正常的“网络”共享。但是,它无法通过webDAV重定向器(桌面体验)模拟webDAV。
 if (imp.impersonateValidUser())
                            {
                                System.IO.File.Copy(local_file, server_file, true);
                                imp.undoImpersonation();
                            }
                            else
                            {
                                throw new Exception("Unable to impersonate for uploading file.");
                            }