我正在构建.net C#控制台程序,以将文件部署到Windows文件共享服务器(正在共享的文件夹)。路径是:: \\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
我想我需要对自己进行身份验证。我遇到过这个:
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();
我不确定如何使用它。当我运行应用程序时,我得到:
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循环中的代码复制系统可以工作,我已经在我的本地系统上测试了它。
如果有人知道我应该如何使用WindowsIdentity和WindowsIdentity来实现这一点,请赐教。我一直在环顾四周,窗口的文档没什么用。
基本上如何通过登录系统复制到共享的远程目录?
答案 0 :(得分:5)
VB但很容易翻译成C#。在复制之前执行此操作:
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
答案 1 :(得分:5)
如果要对远程计算机进行身份验证以移动文件,可以使用LogonUser功能和WindowsIdentity来模拟用户。
/// <summary>
/// Exécute une fonction en empruntant les credentials
/// </summary>
private T ApplyCredentials<T>(Func<T> func)
{
IntPtr token;
if (!LogonUser(
_credentials.UserName,
_credentials.Domain,
_credentials.Password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
out token))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
try
{
// On doit être impersonifié seulement le temps d'ouvrir le handle.
using (var identity = new WindowsIdentity(token))
using (var context = identity.Impersonate())
{
return func();
}
}
finally
{
CloseHandle(token);
}
}
// ...
if (_credentials != null)
{
return this.ApplyCredentials(() => File.Open(path, mode, access, share));
}
return File.Open(path, mode, access, share);