2012-05-02 13 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を持っているので、完全なパーミッションを持つユーザを配置し、彼の詳細を上記のコードに入れてください。 –

関連する問題