Upload file to a folder on other asp.net mvc website - c#

I have two asp.net MVC websites. One the front end(mysite.com) and one the backend(admin.mysite.com).
They both use the same database and everything is working fine. But, I am facing the issue in upload. I want to upload images in front end content folder from the admin website. How can that be achieved?
Using Server.MapPath("~/Content/Images/Products/product") locates the folder in admin website.

If you have access to the iis server, you could set up a virtual directory that is pointed at the desired folder on your front end site.
All that would be required from the front end is that you point the upload path to the virtual directory.

Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application. The leading ~/ tells ASP.Net to look at the root of your application.
To use a folder outside of the application you can use the full path:
#"E:\Project\Folders\Content\Images\Products\product"
You can also get the full path directory:
string currentDirectory = Directory.GetCurrentDirectory();

Related

How to redirect browser to shared files or directories using absolute path within ASP.NET Core and authenticated through Active Directory?

Background: The division I work at within my company has a company intranet webpage with quick links to files and directories located on our local server, and within a shared drive from another server at a different location within the company. The users have the shared drive mapped on their machine using their credentials, and then they can use the home web page to quickly navigate to common files or directories. This worked fine for many years with IE, but it does not in Edge or other common browsers due to a security violation: "Not allowed to load local resource: " Now that IE's support is ending soon we want to get the links working in Edge to use.
Our ASP.NET Core application runs within IIS, and on our local server. Alternatively, I can use an action method instead of the absolute path to a file on the local server since the application is hosted on it. I do this by using the drive letter and full path to the file to read into a byte array and return as a File. I do not know how to do this for the shared server, where users can download files and open up a folder within the browser.
Currently we have static links to a file like: File
or Directory: Directory
I can copy the absolute path into any browser: file://///shared-server-name/Folder and it will show me the folder and its contents. I just can't do it through the HTML markup outside of IE.
What I've tried so far:
Instead of the static links, I tried redirecting to the absolute path using an action method in the controller:
public ActionResult GetFile()
{
redirect("shared-server-name/Folder")
}
This returns an error in the browser: "It looks like the webpage at https://localhost:*****/home/getfile might be having issues, or it may have moved permanently to a new web address."
I tried doing impersonation to see if it was because of the app identity the application was using:
IPrincipal p = _httpContextAccessor.HttpContext.User;
if (p.Identity is WindowsIdentity wid)
{
await WindowsIdentity.RunImpersonated(wid.AccessToken, () => {
bool exists= Exists("shared-server-name/Folder");
log.Debug(exists);
return Task.CompletedTask;
});
}
returns false for exists.
I understand the reason for the security error, but because this is an intranet site, and the access to the shared drive is only through users who are provisioned to it, we'd like to keep the same setup within Edge going forward.
From this blog written by EricLaw (Edge PM), we can know that
For security reasons, Microsoft Edge 76+ and Chrome impose a number of restrictions on file:// URLs, including forbidding navigation to file:// URLs from non-file:// URLs.
The behavior is by design in Edge and there's no option to disable this navigation blocking in Edge. The only thing you can do is using one of the three workarounds listed in the blog:
Open the website in IE mode.
Use extensions like Enable local file links.
Enable group policy IntranetFileLinksEnabled for Edge 95+.
For the group policy, please note that https://localhost/ is considered internet zone by default and can't be configured by the policy.

How to restrict json files that are stored under wwwroot folder to view from Web Browser

I have a "client-styles.css" in my wwwroot / client_folder folder. When I run my project in localhost and give that css file path in the browser its showing the entire css file in the web browser.
( localhost:5000/client_folder/client-styles.css ).
In the same way I have a folder also contains json files which contains sensitive information and those are also showing if we give the path to those Json files ( localhost:5000/client_folder/client-secrets.json ) in the browser. Is there any way restricting some files to view from web browser.
Thanks
Based on your last comment above:
It's perfectly acceptable to store css and javascript files in wwwroot. However, do not store anything secret there. Storing secrets like connectionstrings are best in EnvironmentVariables.

How to give URL for a file stored in app_data folder in asp.net

I have a zip file stored in app_data folder .I want to give the path ofthat file as URL for a hyperlink.How to assign it
Server.Mappath(~/App_Data/Test.zip) will give the physical location as D:/Projects/Mnv/App_Data/Test.zip , but i want to give it as URL like http://..../test.zip, so user can download
You should not use App_Data available to the users.
This is a special folder used to store internal data only (XML, sdf, etc..).
By default, this folder is not even available via Http.
Yo can create an Action in your Asp.Net MVC and use it to retun some file from this folder. A direct download, however, it is not recommended.

Translate known physical path into virtual path

I want to take files from a known location on disk and have ASP redirect to them from code-behind, allowing the browser/app/device to control how the content is displayed.
I tried using:
Server.Redirect(pathToFile);
But got the following exception: Invalid path for child request 'C:\ContentFolder\testImage.png'. A virtual path is expected.
How can I allow my site to redirect users to these files? I am storing the base directory in the web config, and the file names are stored in a database.
If the files reside outside of your website directory you can't redirect to them. Think about the security implications if that was possible. You have a few options
Move the files inside the website directory (or a sub-directory of it). Then you can redirect your users to them using a virtual path e.g. Server.Redirect("~/files/somefile.zip").
Set up a virtual directory in IIS that maps to the physical location of the files on disk. Then you can redirect to them using the virtual path. You can do this through the GUI or config file.
Create a HttpHandler that loads the file from disk and returns them in the response. You can use a querystring to identify the file to load e.g. /filehandler.ashx?filename=somefile.zip. A quick google revealed this example.

HttpHandler and folder locking problemin IIS7

I have a simple HttpHandler which gets the image file from the specified path in the callig URL. For instance, when this URL: http://www.abc.com/images/imageview.ashx?fileName=ok.jpg is called, it will write the file from the path: d:\images\ok.jpg using response.writefile.
Everything works fine, but the folder named images in the web site root path (d:\inetpub\wwwroot\images) is locked and after the first call, I could not rename or delete the folder. I don't use the folder (in the web site root) any where!
I've checked all files for the second folder and they are not in used, only the folder is in used.
When I changed the calling URL to http://www.abc.com/imageview.ashx?fileName=images/ok.jpg the problem resolved.
It seems that IIS7 locks the virtual folder for ashx URL.
Any idea or experience?
It's because the image is resolving most likely relative to the folder the handler is in. Either put an absolute path, like /images/ok.jpg, or on the server call ~/images/ok.jpg to resolve it properly. See more about ~ here, http://weblogs.asp.net/fmarguerie/archive/2004/05/05/avoiding-problems-with-relative-and-absolute-urls-in-asp-net.aspx

Categories