我构建了一个.net核心应用程序(2.0),用于将文件从一个服务器复制到另一个服务器 System.io看起来像这样:
if (!File.Exists(DestinationFullPath)) //copy only if file not exists
{
try {
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Information, "Start copying file from :{0} to {1} ",
pFromPath, pToPath);
File.Copy(file, DestinationFullPath);
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Information, "{0} copied to {1}",
file, pToPath);
} catch (Exception) {
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Critical, "Error in copying {0} to {1}",
file, pToPath);
}
}
问题是,当我在Linux容器上运行时,它需要具有权限的特定用户。
我知道模仿但不知道核心2.0,有没有人知道如何执行它?
有些事情:
var Impersonation = new XXX();
Impersonation.username = "XXX";
Impersonation.Password = "XXX";
Impersonation.Domain = "XXX";
Using(Impersonation) {
//copy only if file not exists
if (!File.Exists(DestinationFullPath)) {
try {
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Information, "Start copying file from :{0} to {1} ",
pFromPath, pToPath);
File.Copy(file, DestinationFullPath);
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Information, "{0} copied to {1}",
file, pToPath);
} catch (Exception) {
runner.WriteLogs(MethodBase.GetCurrentMethod().ToString(),
LogLevel.Critical, "Error in copying {0} to {1}",
file, pToPath);
}
}
}