在.net核心中复制带有身份验证的文件

时间:2018-05-30 14:05:12

标签: c# .net-core impersonation

我构建了一个.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);
        }
    }
}

0 个答案:

没有答案