使用凭据将文件复制到相同或不同域的远程服务器上

使用凭据将文件复制到相同或不同域的远程服务器上

本文介绍了使用凭据将文件复制到相同或不同域的远程服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


请为我提供一种使用远程服务器的用户帐户将文件复制到不同域的远程服务器上的方法.

谢谢inadvance ...

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(...) ?


pls guide me a way to copy file onto remote server of different domain with remote server''s user account.

Thanks inadvance...

推荐答案

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Public Class Form1
    <DllImport("advapi32.DLL", SetLastError:=True)> _
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _
        ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
        ByRef phToken As IntPtr) As Integer
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim admin_token As IntPtr
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim wid_admin As WindowsIdentity = Nothing
        Dim wic As WindowsImpersonationContext = Nothing
        Try
            MessageBox.Show("Copying file...")
            If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then
                wid_admin = New WindowsIdentity(admin_token)
                wic = wid_admin.Impersonate()
                System.IO.File.Copy("C:\right.bmp", "\\157.60.113.28\testnew\right.bmp", True)
                MessageBox.Show("Copy succeeded")
            Else
                MessageBox.Show("Copy Failed")
            End If
        Catch se As System.Exception
            Dim ret As Integer = Marshal.GetLastWin32Error()
            MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString())
            MessageBox.Show(se.Message)
        Finally
            If wic IsNot Nothing Then
                wic.Undo()
            End If
        End Try
    End Sub
End Class



这篇关于使用凭据将文件复制到相同或不同域的远程服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:59