2014-10-07 3 views
0

SFTP를 사용하여 한 서버에서 다른 서버로 파일을 전송하려고합니다. 내가 수동으로 명령이 명령을 사용하고 언제 제대로 작동을 프롬프트 명령SFTP 사용 중 오류

sftp2 -i "public key path" "source file path" [email protected] "destination file path" 

를 사용하고 있지만이 서비스에 대해 지정된 C# 코드 난에 geting하고 오류

계정에 동일한 명령을 사용하고 동일한 프로세스에서 실행중인 다른 서비스에 대해 지정된 계정과 다릅니다.

나는 왜이 오류와 동일한 해상도를 얻는 지 알기 쉽게 설명합니다.

미리 감사드립니다.

답변

0

내 생각 엔 SFTP를 호출 할 때 다른 사용자 (사용자 이름 @ 서버 이름)로 가장해야 할 수도 있습니다. ???

using (new Impersonation(domain, username, password)) 
{ 
    // do the Sftp call. 
} 

이 코드는 Matt Johnson이 사용합니다.

using System; 
using System.Runtime.ConstrainedExecution; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Security.Permissions; 
using System.Security.Principal; 
using Microsoft.Win32.SafeHandles; 

namespace MyApplication 
{ 
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
    public class Impersonation : IDisposable 
    { 
     private readonly SafeTokenHandle _handle; 
     private readonly WindowsImpersonationContext _context; 

     const int LOGON32_LOGON_NEW_CREDENTIALS = 9; 

     public Impersonation(string domain, string username, string password) 
     { 
      var ok = LogonUser(username, domain, password, 
          LOGON32_LOGON_NEW_CREDENTIALS, 0, out this._handle); 
      if (!ok) 
      { 
       var errorCode = Marshal.GetLastWin32Error(); 
       throw new ApplicationException(string.Format("Could not impersonate the elevated user. LogonUser returned error code {0}.", errorCode)); 
      } 

      this._context = WindowsIdentity.Impersonate(this._handle.DangerousGetHandle()); 
     } 

     public void Dispose() 
     { 
      this._context.Dispose(); 
      this._handle.Dispose(); 
     } 

     [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
     private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken); 

     public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid 
     { 
      private SafeTokenHandle() 
       : base(true) { } 

      [DllImport("kernel32.dll")] 
      [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] 
      [SuppressUnmanagedCodeSecurity] 
      [return: MarshalAs(UnmanagedType.Bool)] 
      private static extern bool CloseHandle(IntPtr handle); 

      protected override bool ReleaseHandle() 
      { 
       return CloseHandle(handle); 
      } 
     } 
    } 
}