【问题标题】:copy files with authentication in c#在 C# 中复制具有身份验证的文件
【发布时间】:2013-07-21 02:19:57
【问题描述】:

我正在尝试将文件从本地驱动器复制到服务器上的文件夹之一。服务器上文件夹的名称是“DBFiles”。除了用户名 'user' 和密码 'password1!' 之外,没有人可以访问它

在处理文件之前,如果目录不存在,它也会创建目录。

在创建目录'Test'然后复制文件时,有人可以帮助获得访问权限吗?

if (!Directory.Exists(@"\\server-a\copiedfiles\"))
    Directory.CreateDirectory(@"\\server-a\DBFiles\"+Test);   
File.Copy("C:\Temp\abc.txt", @"\\server-a\DBFiles\");

这是c#的原始代码。

NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
NetworkShare.ConnectToShare(@"\\server-a\DBFiles", "user1", "password1!"); //Connect with the new credentials

File.Copy(@"c:\temp\T1.txt", @"\\server-a\DBFiles\T1.txt");

NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", false); //Disconnect from the server.

它给出的错误是拒绝访问。

【问题讨论】:

  • 你有什么问题吗?不知道如何对共享进行身份验证?运行时出现异常?文件副本没有处理所有字节?
  • 您有权限问题吗?也许你应该在 DBFiles 文件夹的权限下运行你的应用程序
  • 刚刚注意到,您的Exists 检查和您的CreateDirectory 命令正在与\\server-a 上的两个不同共享进行通信,这应该发生吗?
  • 我在尝试复制文件时收到拒绝访问错误消息。在需要身份验证访问之前它工作正常。这里我需要输入用户名和密码来访问文件夹。

标签: c# .net


【解决方案1】:

您可以使用模拟来更改线程用户上下文:

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
    int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
...
LogonUser(userName, domainName, password,
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
            out safeTokenHandle);
...
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
            {
                using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                {

                    // Check the identity.
                    Console.WriteLine("After impersonation: "
                        + WindowsIdentity.GetCurrent().Name);
                    //Do your coping here
                }
            }

MSDN Sample windowsimpersonationcontext

【讨论】:

  • 我必须在这里做什么。没看懂。
  • 您看过 msdn 示例吗?
  • @user1065542 您将第一部分添加到您的课程中。第 2 部分是您在函数中用于建立模拟和输出 safeTokenHandle 的内容。第 3 部分紧随其后,并使用该令牌为您提供来自该令牌的 WindowsIdentity,这是新登录用户的身份代表。然后,Windows 需要该WindowsIdentity 的上下文,当.Impersonate() 在该WindowsIdentity 上运行时,以WindowsImpersonationContext 的形式运行。 Console.WriteLine 语句所在的位置是您进行文件复制的位置。
  • 不过,为了运行此代码,您还需要在类的顶部添加 private static IntPtr safeTokenHandle = new IntPtr(0); const int LOGON32_LOGON_INTERACTIVE = 2; const int LOGON32_PROVIDER_DEFAULT = 0; ,以及 userNamedomainName 和 @987654333 的变量@ 传递给LogonUser(),就像第二个 sn-p 中所示。
【解决方案2】:

另一种选择是您可以通过编程方式访问 Windows 的 NET USE api 并对共享进行身份验证,就像您在资源管理器中访问它并输入凭据一样。

public static class NetworkShare
{
    /// <summary>
    /// Connects to the remote share
    /// </summary>
    /// <returns>Null if successful, otherwise error message.</returns>
    public static string ConnectToShare(string uri, string username, string password)
    {
        //Create netresource and point it at the share
        NETRESOURCE nr = new NETRESOURCE();
        nr.dwType = RESOURCETYPE_DISK;
        nr.lpRemoteName = uri;

        //Create the share
        int ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);

        //Check for errors
        if (ret == NO_ERROR)
            return null;
        else
            return GetError(ret);
    }

    /// <summary>
    /// Remove the share from cache.
    /// </summary>
    /// <returns>Null if successful, otherwise error message.</returns>
    public static string DisconnectFromShare(string uri, bool force)
    {
        //remove the share
        int ret = WNetCancelConnection(uri, force);

        //Check for errors
        if (ret == NO_ERROR)
            return null;
        else
            return GetError(ret);
    }

    #region P/Invoke Stuff
    [DllImport("Mpr.dll")]
    private static extern int WNetUseConnection(
        IntPtr hwndOwner,
        NETRESOURCE lpNetResource,
        string lpPassword,
        string lpUserID,
        int dwFlags,
        string lpAccessName,
        string lpBufferSize,
        string lpResult
        );

    [DllImport("Mpr.dll")]
    private static extern int WNetCancelConnection(
        string lpName,
        bool fForce
        );

    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public int dwScope = 0;
        public int dwType = 0;
        public int dwDisplayType = 0;
        public int dwUsage = 0;
        public string lpLocalName = "";
        public string lpRemoteName = "";
        public string lpComment = "";
        public string lpProvider = "";
    }

    #region Consts
    const int RESOURCETYPE_DISK = 0x00000001;
    const int CONNECT_UPDATE_PROFILE = 0x00000001;
    #endregion

    #region Errors
    const int NO_ERROR = 0;

    const int ERROR_ACCESS_DENIED = 5;
    const int ERROR_ALREADY_ASSIGNED = 85;
    const int ERROR_BAD_DEVICE = 1200;
    const int ERROR_BAD_NET_NAME = 67;
    const int ERROR_BAD_PROVIDER = 1204;
    const int ERROR_CANCELLED = 1223;
    const int ERROR_EXTENDED_ERROR = 1208;
    const int ERROR_INVALID_ADDRESS = 487;
    const int ERROR_INVALID_PARAMETER = 87;
    const int ERROR_INVALID_PASSWORD = 1216;
    const int ERROR_MORE_DATA = 234;
    const int ERROR_NO_MORE_ITEMS = 259;
    const int ERROR_NO_NET_OR_BAD_PATH = 1203;
    const int ERROR_NO_NETWORK = 1222;
    const int ERROR_SESSION_CREDENTIAL_CONFLICT = 1219;

    const int ERROR_BAD_PROFILE = 1206;
    const int ERROR_CANNOT_OPEN_PROFILE = 1205;
    const int ERROR_DEVICE_IN_USE = 2404;
    const int ERROR_NOT_CONNECTED = 2250;
    const int ERROR_OPEN_FILES = 2401;

    private struct ErrorClass
    {
        public int num;
        public string message;
        public ErrorClass(int num, string message)
        {
            this.num = num;
            this.message = message;
        }
    }

    private static ErrorClass[] ERROR_LIST = new ErrorClass[] {
        new ErrorClass(ERROR_ACCESS_DENIED, "Error: Access Denied"), 
        new ErrorClass(ERROR_ALREADY_ASSIGNED, "Error: Already Assigned"), 
        new ErrorClass(ERROR_BAD_DEVICE, "Error: Bad Device"), 
        new ErrorClass(ERROR_BAD_NET_NAME, "Error: Bad Net Name"), 
        new ErrorClass(ERROR_BAD_PROVIDER, "Error: Bad Provider"), 
        new ErrorClass(ERROR_CANCELLED, "Error: Cancelled"), 
        new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"), 
        new ErrorClass(ERROR_INVALID_ADDRESS, "Error: Invalid Address"), 
        new ErrorClass(ERROR_INVALID_PARAMETER, "Error: Invalid Parameter"), 
        new ErrorClass(ERROR_INVALID_PASSWORD, "Error: Invalid Password"), 
        new ErrorClass(ERROR_MORE_DATA, "Error: More Data"), 
        new ErrorClass(ERROR_NO_MORE_ITEMS, "Error: No More Items"), 
        new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "Error: No Net Or Bad Path"), 
        new ErrorClass(ERROR_NO_NETWORK, "Error: No Network"), 
        new ErrorClass(ERROR_BAD_PROFILE, "Error: Bad Profile"), 
        new ErrorClass(ERROR_CANNOT_OPEN_PROFILE, "Error: Cannot Open Profile"), 
        new ErrorClass(ERROR_DEVICE_IN_USE, "Error: Device In Use"), 
        new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"), 
        new ErrorClass(ERROR_NOT_CONNECTED, "Error: Not Connected"), 
        new ErrorClass(ERROR_OPEN_FILES, "Error: Open Files"), 
        new ErrorClass(ERROR_SESSION_CREDENTIAL_CONFLICT, "Error: Credential Conflict"),
    };

    private static string GetError(int errNum)
    {
        foreach (ErrorClass er in ERROR_LIST)
        {
            if (er.num == errNum) return er.message;
        }
        return "Error: Unknown, " + errNum;
    }
    #endregion

    #endregion
}

你使用它就像

DisconnectFromShare(@"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;

ConnectToShare(@"\\server-a\DBFiles", username, password); //Connect with the new credentials

if (!Directory.Exists(@"\\server-a\DBFiles\"))
    Directory.CreateDirectory(@"\\server-a\DBFiles\"+Test);   
File.Copy("C:\Temp\abc.txt", @"\\server-a\DBFiles\");

DisconnectFromShare(@"\\server-a\DBFiles", false); //Disconnect from the server.

【讨论】:

  • 我是否先创建了 [DllImport("Mpr.dll")] 文件?
  • 不,Mpr.dll 是 windows 的一部分,你使用它就像你要调用 User32.dllKernal32.dll 的地方一样
  • 错误 3 找不到类型或命名空间名称“DllImport”(您是否缺少 using 指令或程序集引用?)
  • 您需要在文件顶部包含命名空间using System.Runtime.InteropServices;
  • 您是在\\server-a\copiedfiles\ 还是\\server-a\DBFiles\ 创建目录,您在示例中都使用了这两个目录。另外,您将哪一个作为连接的 url 传入。 ConnectToShare 是否也返回了错误字符串?
【解决方案3】:
NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", true); //Remove this line
NetworkShare.ConnectToShare(@"\\server-a\DBFiles", "user1", "password1!"); //Connect with the new credentials

File.Copy(@"c:\temp\T1.txt", @"\\server-a\DBFiles\T1.txt");

NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", false); //Remove this line also

在谷歌搜索 02 天后,以上内容终于为我工作了。 如果您在第一次登录后使用“NetworkShare.DisconnectFromShare”,则会收到'Access denied' 的错误。然后每次需要重启服务器或者需要执行'net use * /del'命令来删除windows中保存的凭据。

【讨论】:

  • 似乎,然后,就像断开例程存在问题一样 - 强调它不应该被使用 - 不仅仅是在代码 cmets 中。我不得不阅读这篇文章几次才能弄清楚你在说什么。
【解决方案4】:

我已经从远程服务器复制了这个文件。请找到代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace IMPolicy
{
public partial class ExtractData : System.Web.UI.Page
{
    [DllImport("advapi32.DLL", SetLastError = true)]
    public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    protected void Page_Load(object sender, EventArgs e)
    {            

        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        IntPtr token = default(IntPtr);
        if (LogonUser("UserName", "Domain", "Password", 2, 0, ref token) != 0)
        {

            WindowsIdentity identity = new WindowsIdentity(token);
            WindowsImpersonationContext context = identity.Impersonate();

            try
            {                
                File.Copy(@"\\\\10.10.38.25\d$\\Sourav\\Draft Report ITC-LRBD_Online Booking Portal_12082016.pdf", @"d:\\Draft Report ITC-LRBD_Online Booking Portal_12082016.pdf", true);
             }
             finally
             {
                 context.Undo();
             }
        }




    }


  }
}

【讨论】:

    【解决方案5】:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Files_copy_from_Server
    {
        class Program
        {
            static void Main(string[] args)
            {
                NetworkShare.copyfiles();
                Console.ReadLine();
            }
        }
        public static class NetworkShare
        {
            public static  void copyfiles()
            {
                try
                {
                    string username = "username";
                    string password = "Password";
                    DisconnectFromShare(@"\\192.168.11.95\Destination", true); 
    
    //Disconnect in case we are currently connected with our credentials;
                        ConnectToShare(@"\\192.168.11.95\Destination", username, password); //Connect with the new credentials
                        if (!Directory.Exists(@"\\192.168.11.95\Destination\"))
                        Directory.CreateDirectory(@"\\192.168.11.95\Destination\");
                        File.Copy(@"D:\OldBackup\CADCallEnvelopBL.cs", @"\\192.168.11.95\Destination\CADCallEnvelopBL.cs");
                        DisconnectFromShare(@"\\192.168.11.95\Destination\", false); 
     //Disconnect from the server.
                    }
                    catch (Exception objError)
                    {
                    Console.Write(objError.Message);
                }
            }
            /// <summary>
            /// Connects to the remote share
            /// </summary>
            /// <returns>Null if successful, otherwise error message.</returns>
            public static string ConnectToShare(string uri, string username, string 
            password)
            {
                //Create netresource and point it at the share
                NETRESOURCE nr = new NETRESOURCE();
                nr.dwType = RESOURCETYPE_DISK;
                nr.lpRemoteName = uri;
            //Create the share
            int ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
    
            //Check for errors
            if (ret == NO_ERROR)
                return null;
            else
                return GetError(ret);
        }
    
        /// <summary>
        /// Remove the share from cache.
        /// </summary>
        /// <returns>Null if successful, otherwise error message.</returns>
        public static string DisconnectFromShare(string uri, bool force)
        {
            //remove the share
            int ret = WNetCancelConnection(uri, force);
    
            //Check for errors
            if (ret == NO_ERROR)
                return null;
            else
                return GetError(ret);
        }
    
        #region P/Invoke Stuff
        [DllImport("Mpr.dll")]
        private static extern int WNetUseConnection(
            IntPtr hwndOwner,
            NETRESOURCE lpNetResource,
            string lpPassword,
            string lpUserID,
            int dwFlags,
            string lpAccessName,
            string lpBufferSize,
            string lpResult
            );
    
        [DllImport("Mpr.dll")]
        private static extern int WNetCancelConnection(
            string lpName,
            bool fForce
            );
    
        [StructLayout(LayoutKind.Sequential)]
        private class NETRESOURCE
        {
            public int dwScope = 0;
            public int dwType = 0;
            public int dwDisplayType = 0;
            public int dwUsage = 0;
            public string lpLocalName = "";
            public string lpRemoteName = "";
            public string lpComment = "";
            public string lpProvider = "";
        }
    
        #region Consts
        const int RESOURCETYPE_DISK = 0x00000001;
        const int CONNECT_UPDATE_PROFILE = 0x00000001;
        #endregion
    
        #region Errors
        const int NO_ERROR = 0;
    
        const int ERROR_ACCESS_DENIED = 5;
        const int ERROR_ALREADY_ASSIGNED = 85;
        const int ERROR_BAD_DEVICE = 1200;
        const int ERROR_BAD_NET_NAME = 67;
        const int ERROR_BAD_PROVIDER = 1204;
        const int ERROR_CANCELLED = 1223;
        const int ERROR_EXTENDED_ERROR = 1208;
        const int ERROR_INVALID_ADDRESS = 487;
        const int ERROR_INVALID_PARAMETER = 87;
        const int ERROR_INVALID_PASSWORD = 1216;
        const int ERROR_MORE_DATA = 234;
        const int ERROR_NO_MORE_ITEMS = 259;
        const int ERROR_NO_NET_OR_BAD_PATH = 1203;
        const int ERROR_NO_NETWORK = 1222;
        const int ERROR_SESSION_CREDENTIAL_CONFLICT = 1219;
    
        const int ERROR_BAD_PROFILE = 1206;
        const int ERROR_CANNOT_OPEN_PROFILE = 1205;
        const int ERROR_DEVICE_IN_USE = 2404;
        const int ERROR_NOT_CONNECTED = 2250;
        const int ERROR_OPEN_FILES = 2401;
    
        private struct ErrorClass
        {
            public int num;
            public string message;
            public ErrorClass(int num, string message)
            {
                this.num = num;
                this.message = message;
            }
        }
    
        private static ErrorClass[] ERROR_LIST = new ErrorClass[] {
        new ErrorClass(ERROR_ACCESS_DENIED, "Error: Access Denied"),
        new ErrorClass(ERROR_ALREADY_ASSIGNED, "Error: Already Assigned"),
        new ErrorClass(ERROR_BAD_DEVICE, "Error: Bad Device"),
        new ErrorClass(ERROR_BAD_NET_NAME, "Error: Bad Net Name"),
        new ErrorClass(ERROR_BAD_PROVIDER, "Error: Bad Provider"),
        new ErrorClass(ERROR_CANCELLED, "Error: Cancelled"),
        new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"),
        new ErrorClass(ERROR_INVALID_ADDRESS, "Error: Invalid Address"),
        new ErrorClass(ERROR_INVALID_PARAMETER, "Error: Invalid Parameter"),
        new ErrorClass(ERROR_INVALID_PASSWORD, "Error: Invalid Password"),
        new ErrorClass(ERROR_MORE_DATA, "Error: More Data"),
        new ErrorClass(ERROR_NO_MORE_ITEMS, "Error: No More Items"),
        new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "Error: No Net Or Bad Path"),
        new ErrorClass(ERROR_NO_NETWORK, "Error: No Network"),
        new ErrorClass(ERROR_BAD_PROFILE, "Error: Bad Profile"),
        new ErrorClass(ERROR_CANNOT_OPEN_PROFILE, "Error: Cannot Open Profile"),
        new ErrorClass(ERROR_DEVICE_IN_USE, "Error: Device In Use"),
        new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"),
        new ErrorClass(ERROR_NOT_CONNECTED, "Error: Not Connected"),
        new ErrorClass(ERROR_OPEN_FILES, "Error: Open Files"),
        new ErrorClass(ERROR_SESSION_CREDENTIAL_CONFLICT, "Error: Credential Conflict"),
    };
        private static string GetError(int errNum)
        {
            foreach (ErrorClass er in ERROR_LIST)
            {
                if (er.num == errNum)
                {
                    return er.message;                
                }             
            }
            return "Error: Unknown, " + errNum;
        }
        #endregion
    
        #endregion
    }
    

    }

    【讨论】:

    • 欢迎您,感谢您贡献代码。您能否更新您的答案以包括对您正在做什么的更多解释,并突出显示您的代码中解决特定问题的关键部分?通常,我们会尽量避免将大量代码转储作为答案,因为它们无助于未来的读者学习概念或将它们应用于不同的场景。
    猜你喜欢
    • 2019-03-10
    • 2019-03-23
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多