2012-05-02 3 views
1

서버 경로에 파일을 쓰고 싶습니다.하지만 그렇게하려고 할 때 우리는 예외적 인 권한을 가지고 있습니다. 우리는 서버 경로에 쓸 수있는 권한이있는 응용 프로그램의 ID와 비밀번호를 가지고 있지만 나는이 자격 증명을 전달할 수있는 방법을 모른다 :서버 경로에 파일을 쓰는 중 자격 증명을 전달하는 방법은 무엇입니까?

내 현재 코드 :

//Create a new GUID, extract the extension and create a new unique filename 
string strFileGUID = System.Guid.NewGuid().ToString(); 
string extension = Path.GetExtension(attachment.AttachmentFileName); 
string tempfilename = strFileGUID + extension; 

string path = "ServerPath"; 

//Open a filestream and write the contents of the file at server path 
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); 
fs.Write(fileContent.Content, 0, fileContent.Content.Length); 
fs.Flush(); 
fs.Close(); 

답변

0
[DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool LogonUser(
      string lpszUsername, 
      string lpszDomain, 
      string lpszPassword, 
      int dwLogonType, 
      int dwLogonProvider, 
      out IntPtr phToken); 

예 :

IntPtr userToken = IntPtr.Zero; 

bool success = External.LogonUser(
    "john.doe", 
    "domain.com", 
    "MyPassword", 
    (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2 
    (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0 
    out userToken); 

if (!success) 
{ 
    throw new SecurityException("Logon user failed"); 
} 

using (WindowsIdentity.Impersonate(userToken)) 
{ 
    // put your code here 
} 
+0

컴파일되지 않았습니다. '외부'이름이 현재 컨텍스트에 없습니다. –

+0

이 사용자에게 필요한 권한은 무엇입니까? 처음에는 –

+0

@ thabet084 예외가 있으므로 전체 권한을 가진 사용자를두고 위의 코드에 세부 정보를 입력하십시오. –

관련 문제