how to grant write permissions to an web api application in IIS? - c#

I simply have a web api application on IIS server that simply writes to text file on C:\FileStorage\test.txt however when I call this webservice I get the internal 500 error, after expanding the error,I get the following error, how do I grant write access to this web service on IIS server?
"ExceptionMessage": "Access to the path 'C:\\FileStorage\\test.txt' is denied.",
"ExceptionType": "System.UnauthorizedAccessException",
"StackTrace": " at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n at

Grant permissions for that folder to the application pool. So you'd go to the C:\FileStorage folder in Windows and edit permissions, and add IIS APPPOOL\appPoolNameHere as a user, then give it full permissions.
Hope that helps.
EDIT
Step by Step Instructions...
-Open Windows Explorer
-Browse to your folder
-Right click the folder and go to Properties
-On the Security tab click Edit
-Click Add
-Under Locations, make sure it is pointing at your local machine, not a domain
-For the object name, enter below but replace MyAppPool with the name of your application pool...
IIS APPPOOL\MyAppPool
-Set the permissions to Full, or just add Write, or whatever you need.

or you can repalce APPPOOL to the name of your server or computer and with the IIS_ISURS.
example:
yourserver\IIS_IUSRS
See this Screen Shot

Related

Copy file from one server path to another showing access denied

Copy file from one server path to another showing access denied though I have given full access.
Error:System.UnauthorizedAccessException: Access to the path is
denied. at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath) at System.IO.File.InternalMove(String sourceFileName,
String destFileName, Boolean checkHost)
I have tried give security access to the required folder
string sourceFile = "C:/inetpub/wwwroot/MATS_History_Load_App/Python_Scripts/MATSUpgrade.xlsx";
string destinationFile = "//192.xxx.47/Mats/mats_test/MATSUpgrade.xlsx";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
Important: The application is hosted on IIS
given there is no access problem with the identity of your code's process:
this works perfectly well out of the box, try to replace slash with Backslashs and use the # before the path string:
string sourceFile = #"C:\temp\test.txt";
string destinationFile = #"\\fileshare\users\xy\prime.txt";
System.IO.File.Move(sourceFile, destinationFile);
otherwise in service or web hosting scenarios, you need to consider the access rights of your executing identity:
https://support.microsoft.com/en-us/help/207671/how-to-access-network-files-from-iis-applications
As far as I know, if you want to modify or access the network share folder. You should make sure you have choose the right application pool identity permission firslty. If you use the Network Service account as the identity, you still need to add enough permission on the shared folder, Since Network service and local system both appear on the network as the computer account (DOMAIN\computer$).
Normally, we will select the network servcie.
Details, you could refer to below steps:
1.Open the IIS management console, select the right application pool and click the advanced settings.
2.Select the identity and modify the account to networkserivce
3.Right cick the computer and click the property, you will find your computer name as below:
4.Go to your shared folder server, right click the folder and click property. Then you could add your server's account to it like this (Domain\Servername$)

Storing a file in an existing/ created folder keep saying access denied

trying to upload a file to an already created folder but this error keeps coming
Access to the path 'C:\Users\Joe\Documents\Visual Studio
2015\Projects\training\Site1\Site1\Content\ProductImages' is denied.
and then below is this
Exception Details: System.UnauthorizedAccessException: Access to the
path 'C:\Users\Joe\Documents\Visual Studio
2015\Projects\training\Site1\Site1\Content\ProductImages' 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. ASP.NET has a base process identity (typically
{MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and
the configured application pool identity on IIS 7.5) that is used if
the application is not impersonating. If the application is
impersonating via , the identity will be
the anonymous user (typically IUSR_MACHINENAME) or the authenticated
request user.
To grant ASP.NET access to a file, right-click the file in File
Explorer, choose "Properties" and select the Security tab. Click "Add"
to add the appropriate user or group. Highlight the ASP.NET account,
and check the boxes for the desired access.
Source Error:
Line 73: var path = Server.MapPath("~/Content/ProductImages"); // guardo en la variable path la direccion donde quiero guardar las imagenes, este path es un string que tu lo conformas segun el interes, puedeser upload/nombre_fichero/fecha/etc
Line 74: string pathdir = Path.Combine(path, imageName);
*Line 75: file.SaveAs(path); // store file*
Line 76: var imagen = new Image();
Line 77: imagen.ImagePath = pathdir;
and line 75 is in red
When checking the granted rigths there is no restriction for storing or in the file to be stored
The security is coming from the OS, not the compiler. Navigate to your folder in Windows, right-click, change security.
If deploying, be sure to use a folder that is automatically granted write permissions to .NET - like the User/Appdata/Roaming folder.
EDIT:
You are saying you've added EVERYONE to the folder and given them full control but are still getting no joy. Please try this test:
private static void writeText()
{
using (TextWriter tw = new StreamWriter(#"C:myTestFile.txt"))
{
tw.WriteLine("Hello World",false);
tw.Close();
}
}
Run the above from your solution, then check you DEBUG folder for your solution (I've forgotten at this point if we're dealing with console or form or web, so you may have to search your computer for the mytestFile.txt file after running).
The point in running this is that C: (with no backslash) points to your project folder and will automatically have adequate permissions for write. If this works and you get a mytestFile.txt after running, then we go in one direction. If you get an access error, we go in another direction. So please run in your solution or a new console app (your solution would be best) and report back with the results. Have patience buddy - we'll do the best we can to get you where you need to be!
When you host an application in IIS, IIS will run the application under another user name depending on your application pool settings. It is a good idea to know the Application Pool (What is an IIS application pool?), but in your specific case, you do not have to go through the learning curving.
The easiest way is to figure out the actual user name IIS is using by printing out the System.Environment.UserName and grant this user to the directory you want it to access.
The file permission error is coming from your Windows OS Explorer.
It gives you the exact fix:
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Give this a shot.
Try to check the Sharing type of the folder.
Right click->Properties->Sharing Tab->Click Share... Buttong->Then Choose Everyone then add Then change it to read and write.
Then test your program again.
if error still exist try to add the folder to your solution explorer.
Click Solution Explorer->Show All files->Then Find the folder where it was then Include in the project.
Just give it a try.
We have to give write permissions to "IIS_Users" for folder "ProductImages" which are trying to upload, since IIS used this user to access your upload folder.
Please see here how to add IIS_Users to your folder.
Hope this helps you.
Check the permission of that folder, right click on the folder, remove selection from the check box next to "Read Only", then click on "Security" => "Edit" => Select "Users" and tick the check box for "Full Control" and "Apply"

Access to the path \\server\folder\subfolder\..\file_log is denied

I wanted to write the log to the server. The following code is going to create a log file and record the details.
private Log in_log;
in_log = new Log(logfolder + call_date.ToString("yyyyMMdd") + "\\" + call_number + ".log");
But I got an exception.
Access to the path '\\10.50.96.221\inbox\messagelogs\20131105\HZ1_20131105132956319_59.log' is denied.
However I can access the folder \\10.50.96.221\inbox\messagelogs\20131105.
My permission is "Administrators;Remote Desktop Users".
What is wrong?
The windows service runs under its own user. Verify that that user has access to the remote just as your user has. Or change the user to be your user when the service runs.
Your computers are not on a a windows domain, so you have two options. Only 1 of which I have confidance will work:
(this works 99% of the time) Grant "All Users" write permission on the folder
(Never tried it but in theory it should work) In the properties of the service, goto Login, chose "This Account" -> Browser -> Locations. If you see the server computer there you should be able to chose that location and add the admin account from the server as the user the Service runs under.

Unable to read the files from shared folder

I am trying to access files from the shared folder(ftp location) in Asp.net application. It is working fine from visual studio. When i deploy same in IIS 7 , i am getting the following error
"Logon failure: the user has not been granted the requested logon type at this computer."
Stack Trace:
[IOException: Logon failure: the user has not been granted the
requested logon type at this computer. ]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
+9723522 System.IO.FileSystemEnumerableIterator1.CommonInit() +245 System.IO.FileSystemEnumerableIterator1..ctor(String path, String
originalUserPath, String searchPattern, SearchOption searchOption,
SearchResultHandler`1 resultHandler) +556
System.IO.DirectoryInfo.InternalGetFiles(String searchPattern,
SearchOption searchOption) +64 System.IO.DirectoryInfo.GetFiles()
+14
......
My application pool is running in NETWORKSERVICE mode
To which user in IIS i need to give the permission for accessing this folder? is it IUSER or NT AUTHORITY\NETWORK SERVICE ? How can i identify the current running user in IIS?
The reason the connection is refused it most likely a Group Policy which disallows access to that service by the user running the IIS service.
I would not want to give any of those system users permission to access a network resource. Other computers could potentially connect as well.
Instead - make your program authenticate with a guest account or another account which you create and give the appropriate permissions.
I would say that it would be a more secure approach.
You may be trying to log on interactively to a computer you can only access over a network, or vice versa.
Change your logon location. Try to log on either locally (interactively) or remotely (over the network), as appropriate. You may want to ask the person who administers computer security to change the security database so you can log on either locally or remotely.
To debug - look at Environment.UserName at the moment of exception. It will be either user that initiated request OR anonymous user.
To fix: if local files - granting permissions may be ok. If remote and using user's impersonated account - need to impersonate with another account that have permissions on remote machine as you can't use already impersonating account to access remote resource ("NTLM one hop").
Note: make sure you review security requirements when allowing access to files.

Access to file is denied exception with asp.net impersonation

I am trying to access the files with following c# code, but when i try to execute the code i am encountered with exception saying access to path #"\MAHESH-PC\D$\temp\CloudURL.txt is denied .
Impersonation imp = new Impersonation("MAHESH-PC", "mahesh", "welcome");
File.Copy(#"\MAHESH-PC\D$\temp\CloudURL.txt", #"C:\temp\CloudURL.txt", true);
I have given all the access rights to both the temp folders one in the c: drive and other in the d: drive
I have enabled the Impersonation in the IIS , even then i am getting access denied exception , please can i know is their any other settings i should implement to make this working.
Is impersonation enabled in your web.config? I'm not positive but I wouldn't think enabling impersonation in IIS has any affect on the impersonation of your site. If not, turn it on, or grant the app pool identity read/write access to both your directories and see if that helps.
Also, double check your folder system security. It appears you granted everyone read/write access to the share, but if you're navigating to the path \pc\drive$\ then it may be relying on file system security and not the security on the folder share itself. Make sense?

Categories

Resources