2012-05-02 46 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

未編譯名稱'External'在當前上下文中不存在 –

+0

此用戶需要什麼權限?因爲我們仍然有一個例外 –

+0

@ thabet084作爲開始 - 將具有完全權限的用戶放在上面的代碼中。 –

相關問題