Server.Mappath() fail on remote server - c#

i use like this codes for prepare paths.
string path = Server.MapPath("~/"+ User.Identity.Name+ "/file.zip");
i take this error when i run this code on localhost
Index:582 Not allowed to load local resource: file:///C:/Users/trkmml/Documents/GitHub/OfisTakip/trkmml/file.zip
i think its not problem (its chrome security thing). anyway file path is correct.
On the other hand, when I upload the program to hosting, it adds "file:///C:/Inetpub/vhosts" to the front of the link. how can i cancel this part. how do I make it just
http://example.com/httpdocs/trkmml/file.zip".
here is error on remote host
Not allowed to load local resource: file:///C:/Inetpub/vhosts/example.com/httpdocs/trkmml/file.zip

Related

Can't see path to another server in ASP.NET MVC

I have an internal ASP.NET MVC site that needs to read an Excel file. The file is on a different server from the one that ASP.NET MVC is running on and in order to prevent access problems I'm trying to copy it to the ASP.NET MVC server.
It works OK on my dev machine but when it is deployed to the server it can't see the path.
This is the chopped down code from the model (C#):
string fPath = HttpContext.Current.Server.MapPath(#"/virtualdir");
string fName = fPath + "test.xlsm";
if (System.IO.File.Exists(fName))
{
// Copy the file and do what's necessary
}
else
{
if (!Directory.Exists(fPath))
throw new Exception($"Directory not found: {fPath} ");
else
throw new Exception($"File not found: {fName } ");
}
The error I'm getting is
Directory not found:
followed by the path.
The path in the error is correct - I've copied and pasted it into explorer and it resolves OK.
I've tried using the full UNC path, a mapped network drive and a virtual directory (as in the code above). Where required these were given network admin rights (to test only!) but still nothing has worked.
The internal website is using pass through authentication but I've used specific credentials with full admin rights for the virtual directory, and the virtual dir in IIS expands OK to the required folder.
I've also tried giving the application pool (which runs in Integrated mode) full network admin rights.
I'm kind of hoping I've just overlooked something simple and this isn't a 'security feature'.
I found this question copy files between servers asp.net mvc but the answer was to use FTP and I don't want to go down that route if I can avoid it.
Any assistance will be much appreciated.
First, To be on the safe side that your directory is building correctly, I would use the Path.Combine.
string fName = Path.Combine(fPath, "test.xlsm")
Second, I would check the following post and try some things there as it seems to be a similar issue.
Directory.Exists not working for a network path
If you are still not able to see the directory, there is a good chance the user does not have access to that network path. Likely what happened is the app pool running your application has access to the directory on the server. The production box likely doesn't have that same access. You would have to get with the network engineer to get that resolved.
Alternatively, you could write a Powershell script to run as a user who has access to both the production and the development server to copy the file over to the production server if that is your ultimate goal and your server administrators could schedule it for you if that is allowed in your environment.

Failed to load resources

I have application which I deploy to Azure and suddenly I catch one error which wasnt in my local machine when I tested application.
Failed to load resource: the server responded with a status of 404 (Not Found)
SO when I tested application in my local machine everything works perfect without any errors, and when I move to test application live on server many option doesnt work, and in console manager I get this kind of error.
Any help, what can be problem here ?
404 is resource not found error. Most probable reasons are your files are in “../“ folder and you are trying to access file in “../..” folder.
I would suggest using URLs like /Folder/subfolder instead of relative URLs like “../parentfolder/subfolder”..
Also, its good to use “~”..
More on paths here:
https://msdn.microsoft.com/en-us/library/ms178116.aspx

trying to upload a file to Ftp but getting the error: "file name not allowed"!

So I was trying to upload a 1kb text file to my ftp server but this error comes up:
The remote server returned an error: (553) File name not allowed.
so what's wrong with my code?
WebClient upload = new WebClient();
upload.Credentials = new NetworkCredential("******", "*********");
upload.UploadFile("ftp://xxx.com/public_html", "G:/adress.txt");
It's hard to tell, because it's a server error not a code error. However, as currently written, you're trying to upload the file called adress.txt to become a file named public_html. I suspect there's already a directory with that name, and the conflict is preventing the upload. Try
upload.UploadFile("ftp://xxx.com/public_html/adress.txt", "G:/adress.txt");
instead.
This might not apply to you, but if it is a Linux FTP server:
This may help for Linux FTP server.
So, Linux FTP servers unlike IIS don't have common FTP root directory.
Instead, when you log on to FTP server under some user's credentials,
this user's root directory is used. So FTP directory hierarchy starts
from /root/ for root user and from /home/username for others.
So, if you need to query a file not relative to user account home
directory, but relative to file system root, add an extra / after
server name. Resulting URL will look like:
ftp://servername.net//var/lalala
Instead of:
ftp://xxx.com/public_html
You would need a second slash after the server name in addition to the full file name:
ftp://xxx.com//public_html/adress.txt
I ran into this same issue and it fixed it for me.
Source:
Can't connect to FTP: (553) File name not allowed

I have load xml from remote machine

I have url like
http://steepgraph5-pc/sgs/Client/Innovator.xml
from that url I load Innovator.xml into XmlDocument and get node that contain path like
C:\\Program Files (x86)\\Aras\\Innovator\\InnovatorServerConfig.xml
now I have to load InnovatorServerConfig.xml into new XmlDocumnet but this path is local to server and i have to load it from client. I am using c#
C# code is like:
XmlDocument document = new XmlDocument();
document.Load("http://steepgraph5-pc/sgs/Client/Innovator.xml");
string path=document.SelectSingleNode("/ConfigFilePath/#value").Value;
XmlDocument ServerConfigdocument = new XmlDocument();
ServerConfigdocument.Load(path);
There is no way to convert an arbitrary file system path to a means to access that path from a remote computer.
You need to have some sort of network protocol to access it. You appear to have selected HTTP.
Given that you are using HTTP, you have to:
Be wanting to access a file that the server actually makes available
Know how the server maps its Document Root onto the file system
Then you can convert the file path to a URL.
This isn't a sensible thing to do on the client. It would make more sense for the server to rewrite the XML and replace the local path with an HTTP URI.
Innovator server config is just a file loaded by the Innovator server side when it starts up.
If you edit it it will have no effect until you restart the IIS webserver.
I'd recommend just remote desktop to the server, edit the file and restart the webserver.
If you wish to to this remotely, then you can make the Innovator root folder accessible via WEBDAV. This can be mounted or browsed by any remote machine as though it is a local disk.
Then after editing, use the remote management capability of IIS to restart the server.

Upload files to a remote server

I need to upload files from my asp.net (C#) page residing in the web server to a remote server.
I managed to upload files to remote server from localhost using this code:
string serverPath = "\\\\xx.xxx.xx.xx\\Folder\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(serverPath);
But after I published this code to my web server, it stopped working with the error "The network path was not found."
I have looked at a few solutions which suggest using UNC network share and implementing impersonation.
I couldn't figure out how to apply these solutions.
Can someone please give an example, or suggest a simpler solution.
Thanks!!
In FileUpload1.PostedFile.SaveAs(path), path is physical path of file, No Url. You must check:
is Physical folder Exsist?
is You have access to folder?
if answer of both question is true check this code:
string serverPath = #"\\xxx.xxx.xxx.xxx\Folder\";
if (!System.IO.Directory.Exists(serverPath))
System.IO.Directory.CreateDirectory(serverPath);
FileUpload1.PostedFile.SaveAs(serverPath + FileUpload1.FileName);
The account your application runs under must have write permissions to the folder you are trying to upload the file to: \\xx.xxx.xx.xx\Folder\. So you will have to configure the application pool in IIS to run under an account that will have sufficient permissions. Go to the application pool properties in the IIS management console where you will be able to specify an account to be used to run the application. By default it uses a built-in account which won't have any access to shared resources. Take a look at the following article which explains how to do so.
You need a virtual directory on your webserver to upload to. In code you'll have to use Server.Mappath("virtual path") function to get its server path and then save to it.

Categories