Create folder dynamicly on server? What is the right way? - c#

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.

Related

Write a simple text file to IBM iSeries IFS from my ASP.NET web app

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
}

How to transfer a file from a server to another on the same domain using C#

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.

Logon failure: unknown user name or bad password

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);
}

Streamwriter issue with remote machine

I am trying to create a file on the remote machine but I am getting The "Network name cannot be found". I checked the network path and I was able to access the path from my machine. Could you please let me know what could be wrong?
Here is my code.
using (StreamWriter sw = new StreamWriter("\\\\servername\\TEST1\\TEST\\NEWFILE.csv", true))
{
sw.WriteLine(sw);
}
Go to \servername\TEST1 and give write permission to the user or aspnet (if you have a web application) on test folder and then re-run your program. It will work.
To give write permissions, just refer to this article:
How to share a folder/File
In case it still does not work, replace servername with server IP address and do the same as stated above.
Give the access rights to the user under which this application runs either it is a IIS pool or windows service etc
it is surely a security isssue. you need to give Write access to the remote machine

Read file on a remote server

I have a file on a remote server and I want to read this file.
lets say the files location is:
string filePath = #"\\192.168.101.15\c$\program files\xxx\test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
This code is for sure throwing an error:
Logon failure: unknown user name or bad password.
How can I pass my credentials??
if I go start/run and put this path, I need to provide credentials lets say Admin and password 123.
Im using Asp.net, c# 3.5
Any Ideas
You have to use impersonation, ie execute your code with a user who has acces to the shared folder instead of asp.net user :
http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx
You have two way :
-with code
-with configuration
Your application will need to run as a user that has access to the UNC path, or else impersonate a user with such permissions, for the file load operation.
You'd need to be pre-authenticated on the share before you can access the files. It isn't something you can do just by passing a UNC path.
You might consider executing a net use command via the shell programatically. That's the only way I can find to do this.

Categories

Resources