I have been struggling with this problem for hours now. I have an web app that works with Active Directory Authentication. Only certain people are allowed to upload files to the server. When I test from my localhost everything works fine and I can upload files to the correct path on the server. When I publish my solution to the server and I run it from there, it gives me the error "Logon failure: unknown user name or bad password". I have set the permissions on the folder for myself to Full Control and Allow All. The server is running IIS 6. Can some one please advise on what to do. I have tried literally almost everything.
Server is on a different machine.
Local is on my PC.;
if (this.flUpload.HasFile)
{
flUpload.SaveAs(#"SERVERPATH\" + flUpload.FileName);
}
Please try below.
NetworkCredential myCred = new NetworkCredential(GlobalVariablesBO.UserID, GlobalVariablesBO.Password, GlobalVariablesBO.Domain);
WebClient webclient = new WebClient();
webclient.Credentials = myCred;
string tempFileForStorage = Path.Combine(Path.GetTempPath(), Path.GetFileName(dBO.SPFileName));
sharepointUpload.SaveAs(tempFileForStorage);
webclient.UploadFile("ServerPath", "PUT", tempFileForStorage);
webclient.Dispose();
File.Delete(tempFileForStorage);
It looks like more of configuration issue . Make sure that Website hosting server IIS User should have read/write permission to the server where you are uploading files .
Please try below code and see if the file is geeting sabed in tempFileForStorage location. This path is C://Temp
if (this.flUpload.HasFile)
{
string tempFileForStorage = Path.Combine(Path.GetTempPath(), flUpload.FileName
flUpload.SaveAs(tempFileForStorage);
}
Related
So part of my job is to write a file to iSeries IFS, or this path in particular \ServerIPAddress\home\test\
I have an ASP.NET web application to do this, and this is the code (C#) I use to write a simple text file to that directory:
string filename = #"\\SomeIPAdress\home\test\test.txt";
byte[] file = Encoding.ASCII.GetBytes("hello world");
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
fs.Write(file, 0, file.Length);
fs.Close();
When executing this code, the program gives me "Access Denied" error
Exception Details: System.UnauthorizedAccessException: Access to the path '\SomeIPAddress\home\test\test.txt' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity ...
I can access this directory \SomeIPAddress\home\test using windows file explorer using IBM UID and password, and I can create and edit a text file manually as well.
I know it has to have something to do with granting access right to my ASP.NET app by providing that UID and password, but I can't quite figure it out, and I have been stuck for few days.
Let me know if you need any extra information. Thanks for the help
Two solutions.
Best Practice. Setup the iseries to use the same domain controller as everything else on your network. Then it will know the asp.net account requesting access and allow access to the IFS.
or
Setup a mapped drive on the machine hosting the asp.net page that points to the IFS. The mapped drive will have credentials saved in the vault.
Thanks Mike Wills for leading me to a solution. This is the code I use to connect to the network share using P/Invoke WNet Connection, which is from this answer here
DisconnectFromShare(#"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
ConnectToShare(#"\\server-a\DBFiles", username, password); //Connect with the new credentials
if (!Directory.Exists(#"\\server-a\DBFiles\"))
Directory.CreateDirectory(#"\\server-a\DBFiles\"+Test);
File.Copy("C:\Temp\abc.txt", #"\\server-a\DBFiles\");
DisconnectFromShare(#"\\server-a\DBFiles", false); //Disconnect from the server.
Thanks guys for the help.
I think this is what you are looking for. Sorry, I don't have access to my code at work that I know works right now. It was really simple to do and worked perfectly.
If you want my working code, please let me know and I'll pull that tomorrow.
UPDATE: Sorry, I didn't post my code earlier, we are swamped at work.
NetworkCredential nc = new NetworkCredential("user", "password", "domain");
using (new NetworkConnection(#"\\server\directory1\directory2", nc))
{
// your IO logic here
}
I have tried multiple different ways of transferring a file from a server to another different server which is on the same domain.
No matter what I try I keep getting the incorrect username or bad password error, however when I try access the "\serverIP\c$" folder manually from the server I am able to access the folder correctly with the right username and password.
The first part of my code places the file from the local pc to the server where the application is hosted, and this works perfectly :
string path = Path.Combine(Server.MapPath("~/ACAD_Drawings"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
However I then need to move this file from this server onto a different server which will be using the file, and my last attempt was carried out using the following code:
NetworkCredential myCred = new NetworkCredential("Username", "Password", "DomainName");
WebClient webclient = new WebClient();
webclient.Credentials = myCred;
string tempFileForStorage = path;
file.SaveAs(tempFileForStorage);
webclient.UploadFile("\\\\NewServerIP\\c$", "PUT", tempFileForStorage);
webclient.Dispose();
System.IO.File.Delete(tempFileForStorage);
With this code I keep getting the incorrect username and password when I am sure that they are correct. Would anyone know if I am doing anything wrong or missing any steps?
1st in your quest: you need know SMB/CIFS protocol. See : https://en.wikipedia.org/wiki/Server_Message_Block , how to connect(#Steve Drake) : Connect to network drive with user name and password
2nd when you connect on another computer use SMB, you can read/write remote files use normal IO like local computer.
3nd if two servers are not in a same LAN, use other way to transfer files will be better, like socket, WebAPI etc.
I bougth a server on myasp.com, I wanted to create a folder dynamicly to every user in folder that called "UserData". when registering, for now, I create the directory by FTP client and the folder get the username. this method not allways works so I found the traditional method:
Directory.CreateDirectory(Server.MapPath("~") + "hey");
by using this method I get an error :Access to the path 'h:\root\home\sagigamil-001\www\site1\hey' is denied. However, I can check if folder exist.
What should I do? there is a way to give the server access to write to himself? what is the right way?
You probably need to ask your host to give the ASP .NET process write permissions. They might be reluctant to do so because of security reasons. If you can't get such permission, there will be no way for ASP .NET to create the folder.
You can create a directory over FTP by using this snippet:
var request = WebRequest.Create(new Uri("ftp://host/directory"));
request.Method = WebRequestMethods.Ftp.MakeDirectory;
using (var response = (FtpWebResponse)request.GetResponse()) {
Console.WriteLine("Response status code: {0}", response.StatusCode);
}
Don't forget to set your credentials if needed (assign them to request.Credentials).
If you're still running into trouble, don't forget to post the error you're getting.
I have the following code:
var saveFolder = Path.Combine(Properties.Settings.Default.DropBoxFolder, guid.ToString("N"));
// Create folder, if it does not exist (for the first attachment, it shouldn't exist)
if (!Directory.Exists(saveFolder))
{
Directory.CreateDirectory(saveFolder);
}
var saveFilePath = Path.Combine(saveFolder, file.FileName);
file.SaveAs(saveFilePath);
I'm using GUIDs to generate folders for uploads on my IIS server. The .NET web application is configured to impersonate the user. I granted modify permissions to the target folder (it is a local path on the web server) for Domain Users, Local Service and Everyone, but some users still can't upload files. I can and other people on my team can.
The weird part is that the exception says this:
Could not find a part of the path 'C:\Users\USERID\Desktop\FILENAME'
That path is the path to the file the user selected to upload (their local file path). I feel it is safe to say that the user has permission to his own file on his own desktop. I don't use user impersonation much, so I am wondering what I missed in my configuration or permissions. Any suggestions for debugging this issue? Thanks!
Note: the CreateDirectory method works just fine, even when the exception is thrown. I would have thought that if the user didn't have permission the directory creation would have failed first.
What I am trying to accomplish is to upload some files from one domain on my shared hosting to another domain on the same hosting where the files will be displayed. When I debug the application, the process gets to the SaveAs() method and then throws the exception,"Could not find a part of the path ..." .
I have followed these instructions on finding my site's folder's absolute path and I have implemented this path in my code, using the same method I've been using for a good part of my file uploading, and I have never ran into any problems. My read/write permissions are allowed for the folder that I'm attempting to save these files in.
I'm wondering "Is it because I'm trying to upload the file to a different directory?". If so, is there a better way to accomplish this?
var fileName = Path.GetFileName(file.FileName);
var path = #"D:\Hosting\someNumbers\html\SiteFile\SiteImages\" + fileName;
file.SaveAs(path);
myObject.FilePath1 = path;
Any help will be highly appreciated.
As it turns out, my error was more or less a security issue with GoDaddy's hosting. GoDaddy sees this type of action as a "third party FTP request", which is not allowed. In conclusion, GoDaddy does not allow a user to upload a file on one site, and then FTP that file to another site on the same hosting plan.