When I tried to access network drive file from local its working fine but when i deploy the code I am getting bellow error
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Web.HttpResponse.WriteFile(String filename, Boolean readIntoMemory)
at System.Web.HttpResponse.WriteFile(String filename)
at Configs.gvConfigs_RowCommand(Object sender, GridViewCommandEventArgs e) in C:\Users\bpucha1103c\Desktop\CellBackHaul_Publish\Configs.aspx.cs:line 59 2013-02-05 13:31:21,412 [19] WARN System.Web.UI.Page [(null)] - Logging:System.IO.IOException: The account used is a computer account. Use your global user account or local user account to access this server.
how to do impersonation when accessing the file in network shared folder? Below is my code
GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
string strFilename = lnkTxtFile.Text.Replace("/","\\");
System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
Response.ContentType = "application/octet-stream";
Response.WriteFile(targetFile.FullName);
//HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
here is my modified code
GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
string strFilename = lnkTxtFile.Text.Replace("/", "\\");
System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
RunOperationAsUser(() =>
{
//GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
//string strFilename = lnkTxtFile.Text.Replace("/", "\\");
//System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
Response.ContentType = "application/octet-stream";
Response.WriteFile(targetFile.FullName);
//HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}, "bpucha1103c", targetFile.DirectoryName , "White1234");
You can try to use the following code to impersonate as another user and run an action:
now you can do