I tried to upload file to a network path with following codes for silverlight app:
public void ProcessRequest (HttpContext context)
{
//.....
using (FileStream fs = File.Create(@"\\Server\Folder\" + filename))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
}
It’s working fine when I run it in debug mode with VS built-in web server. At SL side, I call this handler with url like:
UriBuilder ub = new UriBuilder("http://localhost:38700/FileUpload.ashx");
Then I publish this app to local IIS and change the url to http://localhost/Mysite/FileUpload.ashx
then run the app again. It won’t work anymore, but no error.
I guess it is because different credential to call File.Create. So I want to use specific credential in handler to put file to the destination.
How to use a credential for File.Create?
I believe you will need to impersonate a user. The code below should do it. Essentially, you gather the domain, user, and password and instantiate the ImpersonationMgr class. Then call the BeginImpersonation method. After that call the corresponding WriteFile method you wish. The WriteFile method will assert if impersonation is enabled. You can follow similar patterns for other file methods such as delete and move.
UPDATE