Access file from another server using asp.net page - c#

In my ASP.NET application I have to show some images. Actually these images saved in some other server. In my database I have the file path. I need to access file from remote server to my application. How can I do that? I am using the following code
imgFiles = Directory.GetFiles(strFullPath,
strPkStock + "_*",
SearchOption.TopDirectoryOnly);
But this throws an error saying "Access to the path denied"

You have to have file system access on other server to do this and I am sure you will not have it.
The shortest way to get it done is, publish the images folder from other server and use the url of the images to access it rather then file system path. This way, your asp.net application will be able to easily access these images. In fact, it will be the client browser which will directly access these images from the other server, you will just change the src of images.

Murtuza Kabul's suggestion to access files over HTTP/HTTPS is probably easiest approach. You may use HTTPS with client ceritficates to prevent other users/machines to read files from the same HTTPS server.
If you want to access files directly on other server's shared folder you need to make sure that code that accesses files runs under account that have at least read permissions for that shared folder.
Note that default configuration (where code runs under either special anonymous account or calling user's credentials) account that code runs under will not have permissions on other servers (anonymous becuse it is local account, user's due to restrictions on delegation also called "NTLM one hop hell").
Your easiest bet is to run app polls under some account that have permissions on other server and de-impersonate current user to process account. You can also explcitly impresonate some account to access remote files.

Related

Create a folder on NTFS server using C# web api

I am implementing an application that will create a folder on the NTFS File Server.
For that, I have set up the distributed environment. Meaning, I have machine on which I have hosted my C# Web API and I have NTFS file server on which I have to create folders.
I want my API to create a folder on NTFS machine. How can I do that?
Do I need to share NTFS Drive/Folder to create a subfolder? If so, then with whom I need to share a NTFS folder (either server user or IIS_USRS)?
Is there any other way to create a folder without sharing a drive/folder on NTFS folder.
When I have done this in the past I have done it through a temporary impersonation. For example...
using (Impersonator impersonate = new Impersonator(#"UserName", "Password", "Domain"))
{
//Create directory or copy files across the network
}
I believe I lifted my impersonator code from A small C# Class for impersonating a User
Case 1:
if web server and file server are in the same domain, you can consider to use a domain user, and created a shared folder on NTFS server and grant full access for the domain user accessing it. (depends on your requirements).
On web server, for the web application pool, set the domain user as the identity/credential to run the pool. so you can easily use IIS management tool to update it if password gets changed. for the authentication, you can use whatever you want based on requirements, but remember while you call the code to create folders on NTFS server, you need to use app pool user. (for example, if you turn on impersonation on a different user in authentication, in your code, you need to do impersonation using the pool user.)
Case 2,
web server and file server are not in the same domain, I usually will set up a ftp server on the file server to allow specific users to access it (creating folders and upload files....). Otherwise when you may need your IT administrator to make File server domain trust web server domain, then you can do the same thing as case 1.
About impersonation code, it could be something like:
//get the identity of an appPool
using(System.Security.Principal.WindowsIdentity wid = System.Security.Principal.WindowsIdentity.GetCurrent())
{
using (System.Security.Principal.WindowsImpersonationContext ImpersonationCtx = wid.Impersonate())
{
//creating folders, uploading files to UNC path...
ImpersonationCtx.Undo();
}
}
If your case is one of this, hope it helps.

Unauthorized Access Exception on IIS7 virtual directory with proper permissions

We have a customer with multiple servers. Let's say the important ones are ServerX - their current production web server, ServerY - their share server, and ServerZ - their new web server they are trying to move to. Today, the .net application on ServerX uses an IIS7 virtual directory to point to a share folder on ServerY. I set up ServerZ's IIS virtual directory as exactly as I could to look the same (same Physical Path, Credentials, Logon type). But the Fileupload.SaveAs(...) method keeps failing with the message "System.UnauthorizedAccessException: Access to the path '\[ServerY][Share]\file.png' is denied."
I have checked permissions. Both ServerX and ServerZ's apppool identity is network service. The share was set up with full access to "Everyone." I even specified Network Service and the Credentials account to have access. The folder is not read only and, again, the site running on ServerX operates saves fine.
To add to my confusion, the application on ServerZ can read and open files from the virtual directory. It only has issue when I try to save a new one. And when logged into ServerZ with the Credentials used for the Virtual Directory, I can create/save files.
I should specify two more things:
I had to do some work to get ServerZ on Active Directory. Maybe I missed something in that step that would only affect the application?
Both ServerY and ServerZ are Server 2012 instances. ServerX is Server 2008.
Let me know if you have any other questions or suggestions. Thank you

C# Asp.NET Internal Web App: Copy file from a different local server

My internal web app is on server //tom... but i need to upload a file from server //jerry..
while fileupload.saveas works just great to bring the file locally on //tom, when i try to send it back to //jerry, it tells me that access is denied. I tried to give permissions to //jerry/users to do everything but still nothing.
what is the user/group that a web app resting on //tom would be ?
in my code behind i even tried FIle.Copy (..). file.move etc.. but it tells me that access is denied to that particular file from //jerry...
what can i do ?
PS: when i run the web app locally (localhost), everything works just fine, the problem happens only when i go live...
This sounds like a permissions issue. When you run the app locally, it's most likely running under your account but on the server it usually defaults to the NetworkService account, which probably isn't in the network domain. You'll likely need to create a domain account to run your application under, either that or impersonate the user's domain account if they have access to the directory.

create directory on fly during application deployment or first request to application.

I All,
I have been stuck in this issue for a week now.I want the directory to be created in the root of my project when my application runs in IIS.But since IIS user account can't be given full permission in the root directory , i get the access denied error while trying to create directory.I tried to find a work around by keeping the images in App_Data folder but does not look like a good approach as per the post enter link description here
I need to create the folder on fly when the request comes to the application for the first time without getting access denied error.It is also ok if i am able to create directory with permission during deployment.
What is the best approach for the above problem to solve,
Thanks S.
You are misunderstanding the information in your link. It's true that the browser shouldn't have access to the App_Data folder, but in your case it's the server, your ASP.NET code that does this.
If this scenario is for letting user upload and download files, and you have sensitive files in the app_data folder, create a dedicated folder for your purpose instead let's say /Uploads.
Create this folder in your project directly and deploy it to your destination web server.
Give the app pool account read/write permissions, and that should do it.
Minimize the number of folders that the app pool account has write permissions to.
If you create a folder /wwwroot/Uploads and give the app pool account full permission to Uploads only, it's not necessary to have full permissions on the root level. The app can create folders dynamically in the /Uploads folder.

Tree View Directory Browser in Asp.net

I am using a Tree View Directory browser in my application. I am using
DirectoryInfo.GetDirectories()
DirectoryInfo.Getfiles()
My requirement is that I need to browse the server folders from client with this. I am able to access shared drives also from the same machine. I hosted the site in IIS6 in a virtual directory. When I try to access the shared drive from the client I dont find it. Do we have to do something in IIS? I tried giving IdentityImpersonate="true" but still getting same problem.
This is a double hop problem. The IIS server would have to do delegation to move your credentials across 2 hops, (your machine to IIS - IIS to file share). Configuring delegation is a tricky process, but I haven't tried in a while, maybe the diagnostics have improved. Normally you follow the delegation instructions and still get access denied.
The easier solution is to use explicit credentials to go from IIS to the file share. The down side to this is that you have to specify username and password of a windows account somewhere in your web.config or source code.

Categories

Resources