使用凭证的网络 IO

发布于 2024-09-02 21:27:52 字数 312 浏览 3 评论 0原文

是否可以将文件从需要凭据的网络位置移动到也需要凭据的另一个网络位置,而不映射任何驱动器。 (即:不使用任何 P/Invoke)

示例:

FileInfo fi = new FileInfo(@"\\SomeComputer\SomeDrive\SomeFolder\someFile.txt");
fi.MoveTo(@"\\AnotherComputer\AnotherDrive\AnotherFolder\AnotherFile.txt");

如果源和目标网络驱动器已映射,则此方法可以正常工作,但如果没有映射,则不会。

Is it possible to move files from a network location that requires credentials to another network location that also requires credentials without mapping any drive. (ie: Without any use of P/Invoke)

Example:

FileInfo fi = new FileInfo(@"\\SomeComputer\SomeDrive\SomeFolder\someFile.txt");
fi.MoveTo(@"\\AnotherComputer\AnotherDrive\AnotherFolder\AnotherFile.txt");

This works fine if the source and destination network drives are already mapped but if they are not It doesn't.

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

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

发布评论

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

评论(2

别闹i 2024-09-09 21:27:52

尝试类似的操作:

    [DllImport("advapi32.dll")]
    private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    /// <summary>
    /// Used for logging on the domain
    /// </summary>
    public enum LogonProvider
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_DEFAULT = 0,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT35 = 1,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT40 = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT50 = 3
    };

    /// <summary>
    /// Used for logging on across the domain
    /// </summary>
    public enum LogonType
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_INTERACTIVE = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK = 3,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_BATCH = 4,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_SERVICE = 5,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_UNLOCK = 6,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NEW_CREDENTIALS = 9
    }
 IntPtr token = new IntPtr();
 LogonUser(<username>, <domain>, <password>, (int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS, (int)LogonProvider.LOGON32_PROVIDER_WINNT50, ref token);
 WindowsIdentity w = new WindowsIdentity(token);
 w.Impersonate();

这会模拟域用户,然后可用于复制文件。

Try something like:

    [DllImport("advapi32.dll")]
    private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    /// <summary>
    /// Used for logging on the domain
    /// </summary>
    public enum LogonProvider
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_DEFAULT = 0,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT35 = 1,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT40 = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT50 = 3
    };

    /// <summary>
    /// Used for logging on across the domain
    /// </summary>
    public enum LogonType
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_INTERACTIVE = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK = 3,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_BATCH = 4,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_SERVICE = 5,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_UNLOCK = 6,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NEW_CREDENTIALS = 9
    }
 IntPtr token = new IntPtr();
 LogonUser(<username>, <domain>, <password>, (int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS, (int)LogonProvider.LOGON32_PROVIDER_WINNT50, ref token);
 WindowsIdentity w = new WindowsIdentity(token);
 w.Impersonate();

This impersonates a domain user, and can then be used to copy files.

烟雨凡馨 2024-09-09 21:27:52

不,您需要 p/调用某些东西。 BCL 中未提供此功能。

No. You'd need to p/invoke something. This functionality is not provided in the BCL.

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