I have a webpage that allows a user to upload a file to a network share. When I launch a webpage locally (in VS 2008) and try to upload a file, it works! However, when I deploy a website to a web server and try to upload a file through a web page, it does not work!
The error returned to me on the web server says: "Access to the path \ 05prd1 \ emp \ test.txt" is denied. Therefore, this is a permission issue.
The network resource is configured to provide full access to me (NT authentication) and NETWORK SERVICE (this is the default .NET account and what we installed in our IIS application pool as the default user for this website).
I tried this with and without impersonation on a web server, and none of the methods work, but both methods work on my local machine (in other words, using and without impersonation on my local machine).
Below is the code that downloads the file. Please note that the code below includes impersonation, but as I said above, I tried it with and without impersonation, and that didn't matter.
if (fuCourses.PostedFile != null && fuCourses.PostedFile.ContentLength > 0) {
System.Security.Principal.WindowsImpersonationContext impCtx;
impCtx =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
try {
lblMsg.Visible = true;
HttpPostedFile file = fuCourses.PostedFile;
string fName = file.FileName;
string uploadPath = "\\\\05prd1\\emp\\";
if (fName.Contains("\\")) {
fName = fName.Substring(
fName.LastIndexOf("\\") + 1);
}
FileInfo fi = new FileInfo(uploadPath + fName);
if (fi != null && fi.Exists) {
fi.Delete();
}
FileStream fs = File.Create(uploadPath + fName, file.ContentLength);
using (Stream stream = file.InputStream) {
byte[] b = new byte[4096];
int read;
while ((read = stream.Read(b, 0, b.Length)) > 0) {
fs.Write(b, 0, read);
}
}
fs.Close();
lblMsg.Text = "File Successfully Uploaded";
lblMsg.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex) {
lblMsg.Text = ex.Message;
lblMsg.ForeColor = System.Drawing.Color.Red;
}
finally {
impCtx.Undo();
}
}
Any help on this would be greatly appreciated!
source
share