0

これは、ログオンを偽装する方法に関する同様の質問です。

ただし、実行しようとしたとき、System.IO.File.Copy()またはSystem.IO.File.Move()偽装中に問題が発生し、次のエラーが表示されます。

ログオンの失敗: 不明なユーザー名または間違ったパスワード

私のコードでは、適切な偽装コードをラップするカスタム クラスを作成したので、次のように呼び出すことができます。

using(var cnn = new {NetworkName}Connection()){
  // Work in the file system under admin privileges
  System.IO.File.Copy("{UNCSourcePath}", "{UNCTargetPath}", true);//Copy file from one server to another, overwrite if necessary
}

このようにして、ID が適切に破棄されるようにします。私のラッパーは以下に掲載されています:

public class {NetworkName}Connection : IDisposable
  {
    [DllImport("advapi32.dll")]
    public static extern int LogonUser(String lpszUserName,String lpszDomain,String lpszPassword,int dwLogonType,int dwLogonProvider,ref IntPtr phToken);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool CloseHandle(IntPtr hObject);

    IntPtr tokenHandle;
    WindowsIdentity newId;
    public WindowsImpersonationContext User { get; set; }
    public {NetworkName}Connection()
    {
      this.tokenHandle = new IntPtr(0);
      if (LogonUser("{UserName}", "{NetworkName}", "{Password}", 9, 3, ref this.tokenHandle) != 0)
      {
        newId = new WindowsIdentity(tokenHandle);
        this.User = newId.Impersonate();
      }else{
        throw new Exception("Couldn't log onto {NetworkName}.");
      }
    }

    public void Dispose()
    {
      this.User.Dispose();
      CloseHandle(this.tokenHandle);
    }
  }

ラッパー内で、ファイルの存在を正常に検証してFileInfoオブジェクトを作成できますが、アプリケーションがコピー機能で停止する理由/修正は何でしょうか?

もう 1 つの重要な注意点は、接続しているサーバーが古い Windows Server 2000 マシンであることです。VB.NET アプリケーションでも同様のコードが動作しているため、ロジックと資格情報が正しいことはわかっています。

4

0 に答える 0