C# 使用其他域/用户名/密码将文件复制到另一个目录

发布于 2024-08-30 17:39:40 字数 146 浏览 1 评论 0原文

在 c#2008 中, 我正在尝试将文件复制到目标路径(例如 \newserver\destinationFolder),该路径可以位于另一个域中或使用与当前用户不同的用户名/密码。在执行 File.Copy(...) 之前,如何指定这些新的网络凭据?

谢谢你!

In c# 2008,
I'm trying to copy a file to a destination path (for example \newserver\destinationFolder), that can be in another domain or using a different username/password than the current user. How can I specify these new network credentials before doing the File.Copy(...) ?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

维持三分热 2024-09-06 17:39:40

好问题,但我认为这是不可能的。我相信这种类型的文件从一个域复制到另一个域(没有建立信任)将被视为安全漏洞。

一些选项:

  1. 在域之间建立信任并向当前用户授予权限
  2. 您可以尝试使用 Shell 执行命令,看看是否可以通过命令行参数来完成此操作。我在想“Net use”命令。

Good question but I dont think this is possible. I believe this type of file copy from one domain to another ( where there is not a trust set up) would be considered security hole.

Some options:

  1. Set up a trust between the domains and give rights to the current user
  2. You could try a Shell execute command and see if you can make this work through command line parameters. I was thinking "Net use" command.
内心旳酸楚 2024-09-06 17:39:40

查看登录用户。这是它的 pinvoke 页面:

http://www.pinvoke.net/default.aspx /advapi32.logonuser

Look at LogonUser. Here's the pinvoke page for it:

http://www.pinvoke.net/default.aspx/advapi32.logonuser

隔岸观火 2024-09-06 17:39:40

地图驱动器 =>以本地用户身份复制文件=>取消映射驱动器。请查看此处

    protected int MapDrive(){
        NETRESOURCEA[] resources = new NETRESOURCEA[1];
        resources[0] = new NETRESOURCEA();
        resources[0].dwType = 1;
        int dwFlags = 1;
        resources[0].lpLocalName = _DriveLetter;
        resources[0].lpRemoteName = _Path;
        resources[0].lpProvider = null; 
        int ret = WNetAddConnection2A(resources, null, null, dwFlags);
        return ret;
    }

    protected int UnmapDrive()
    {
        int ret = WNetCancelConnection2A(_DriveLetter, 0, true);
        return ret;
    }

MapDrive => Copy files as local user => UnmapDrive. Take a look here:

    protected int MapDrive(){
        NETRESOURCEA[] resources = new NETRESOURCEA[1];
        resources[0] = new NETRESOURCEA();
        resources[0].dwType = 1;
        int dwFlags = 1;
        resources[0].lpLocalName = _DriveLetter;
        resources[0].lpRemoteName = _Path;
        resources[0].lpProvider = null; 
        int ret = WNetAddConnection2A(resources, null, null, dwFlags);
        return ret;
    }

    protected int UnmapDrive()
    {
        int ret = WNetCancelConnection2A(_DriveLetter, 0, true);
        return ret;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文