使用 .net core 将文件复制到远程 windows 服务器
Copy File to remote windows server using .net core
我已经放置了接受 zip 文件的端点。端点可以正常获取 zip 文件。现在,在我获得该文件后,我试图从该端点将该文件复制到不同的远程服务器。
我尝试使用下面的代码通过参考 micrsoft 文档 https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:
使用下面的代码连接到远程服务器
bool returnValue = LogonUser(@"ACC\Test.test", "acc.local", "password",
9, 0,
out safeAccessTokenHandle);
if (returnValue)
{
WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
{
//Destination remote server folder to upload file
var pathToUploadFile = @"\152.158.100.45\D\FileUpload";
string fileName;
fileName = file.FileName;
var path = Path.Combine(pathToUploadFile, fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
//save a zip file to folder
file.CopyTo(stream);
}
});
}
在这段代码中,我得到用户名错误,密码在这行代码中不正确:
var stream = new FileStream(path, FileMode.Create)
我什至不确定 LogonUser 方法是否连接到远程服务器。那么有没有更好的方法来将这个文件复制到远程服务器请给出一个在 .net core 6.0 C# 中实现的想法。非常感谢任何帮助。谢谢
最后,我能够通过实施在以下问题中提到的名为 WNetUseConnection 的 Win32 API 解决方案来解决问题:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials
我已经放置了接受 zip 文件的端点。端点可以正常获取 zip 文件。现在,在我获得该文件后,我试图从该端点将该文件复制到不同的远程服务器。 我尝试使用下面的代码通过参考 micrsoft 文档 https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:
使用下面的代码连接到远程服务器 bool returnValue = LogonUser(@"ACC\Test.test", "acc.local", "password",
9, 0,
out safeAccessTokenHandle);
if (returnValue)
{
WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
{
//Destination remote server folder to upload file
var pathToUploadFile = @"\152.158.100.45\D\FileUpload";
string fileName;
fileName = file.FileName;
var path = Path.Combine(pathToUploadFile, fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
//save a zip file to folder
file.CopyTo(stream);
}
});
}
在这段代码中,我得到用户名错误,密码在这行代码中不正确:
var stream = new FileStream(path, FileMode.Create)
我什至不确定 LogonUser 方法是否连接到远程服务器。那么有没有更好的方法来将这个文件复制到远程服务器请给出一个在 .net core 6.0 C# 中实现的想法。非常感谢任何帮助。谢谢
最后,我能够通过实施在以下问题中提到的名为 WNetUseConnection 的 Win32 API 解决方案来解决问题: Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials