通过文件共享、用户身份验证通过网络复制文件 [英] Copy files over network via file share, user authentication

查看:29
本文介绍了通过文件共享、用户身份验证通过网络复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 .net C# 控制台程序以将文件部署到 Windows 文件共享服务器(正在共享的文件夹).路径为:: \\192.168.0.76\htdocs\public

I am building a .net C# console program to deploy file to a windows file share server (folder that is being shared). The path is :: \\192.168.0.76\htdocs\public

运行时出现错误:

[09:35:29]: [Step 1/3] Unhandled Exception: System.UnauthorizedAccessException: Access to the path '\\192.168.0.76\htdocs\public' is denied.
[09:35:29]: [Step 1/3]    at DeployFileShare.Program.CopyDir(String source, String dest, String[] exclude, Boolean overwrite)
[09:35:29]: [Step 1/3]    at DeployFileShare.Program.Deploy(String num, String source)
[09:35:29]: [Step 1/3]    at DeployFileShare.Program.Main(String[] args)
[09:35:29]: [Step 1/3] Process exited with code -532459699

我想我需要验证自己的身份.我遇到过这个:

I think I need to authenticate myself. I've come across this:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();

我也试过:

AppDomain.CreateDomain("192.168.0.76").SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity("user", "pass");
WindowsImpersonationContext context = idnt.Impersonate();

我不知道如何使用它.当我运行应用程序时,我得到:

I am not sure how to use it. When I run the application I get:

C:\Users\Administrator>DeployFileShare 1 R:\BuildOutput\_PublishedWebsites\Web 2
1
Deploy Started Web, version 21
-- Deploy Prepared
-- Deploying to 1

Unhandled Exception: System.Security.SecurityException: There are currently no l
ogon servers available to service the logon request.

   at System.Security.Principal.WindowsIdentity.KerbS4ULogon(String upn)
   at System.Security.Principal.WindowsIdentity..ctor(String sUserPrincipalName,
 String type)
   at DeployFileShare.Program.Authenticate(String server)
   at DeployFileShare.Program.Deploy(String num, String source)
   at DeployFileShare.Program.Main(String[] args)
The Zone of the assembly that failed was:
MyComputer

基本代码如下:

static void Main()
{
Copy();
}
static void Copy()
{
AppDomain.CreateDomain(GetServerInfo(server, "server")).SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            WindowsIdentity idnt = new WindowsIdentity(GetServerInfo(server, "user"), GetServerInfo(server, "pass"));
            WindowsImpersonationContext context = idnt.Impersonate();
string source = "C:\\someDir";
string dest = "\\192.168.0.76\shareFolder"
string[] sourceFiles = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
            foreach (string file in sourceFiles)
            {
                string local = file.Replace(source, "");
                if (exclude.Contains(local))
                    continue;
                if (!Directory.Exists(Path.GetDirectoryName(dest + "\\" + local)))
                    Directory.CreateDirectory(Path.GetDirectoryName(dest + "\\" + local));
                File.Copy(file, dest + "\\" + local, overwrite);
                Console.WriteLine("-- -- [copied] {0} -> {1}", file, dest + "\\" + local);
            }
}

for 循环中的代码复制系统有效,我已在本地系统上对其进行了测试.

The code copy system in the for loop works, I've tested it on my local system.

如果有人知道我应该如何使用 WindowsIdentity 和 WindowsIdentity 使其工作,请赐教.我一直在环顾四周,window 的文档并没有多大帮助.

If anyone knows how I should use WindowsIdentity and WindowsIdentity to get this to work please enlighten me. I've been looking around and window's documentation doesn't help much.

基本上如何通过登录系统复制到正在共享的远程目录?

Basically how can I copy to a remote directory being shared by logging into the system?

推荐答案

VB 但很容易转换为 C#.在复制之前执行此操作:

VB but easily translated to C#. Do this before your copy:

Private Sub Open_Remote_Connection(ByVal strComputer As String, ByVal strUserName As String, ByVal strPassword As String)
    Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
    ProcessStartInfo.FileName = "net"
    ProcessStartInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword
    ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
    System.Diagnostics.Process.Start(ProcessStartInfo)
    System.Threading.Thread.Sleep(2000)
End Sub

这篇关于通过文件共享、用户身份验证通过网络复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆