Can't write files on EC2 machine due to permissions - c#

I am facing an issue where I am running .NET core app running on IIS and I need to read/write files locally on the machine but I am getting a runtime error when I try to access these files.
Access to the path 'C:\mydata\somedata'
is denied.
to avoid this issue I gave permissions to IIS_IUSRS user to read and write on the specified folder on the machine by connecting remotely to the machine and do that and everything is working okay right now.
The problem is that machine is running on aws beanstalk environment so every time that machine restarts a new instance is created without any permissions to IIS_IUSRS user and I need to go again and do it manually
my question, Is there anything I can do to not go through all that hassle of connecting to machine remotely and giving permissions manually?

I made a workaround in my Startup file by adding the following to app middlewares
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, #"mydata/somedata")),
RequestPath = #"/SomeExampleRequestPath",
EnableDirectoryBrowsing = false
});
It allows you to read write files located locally on the machine without needing to give IIS read-write permissions

Related

UNC path is invalid when starting ASP.NET Core Application in IIS

I have a shared drive on my local network \hostname\shared\resource. I have mounted the drive programattically from within the Asp.net core web application in the startup.cs file.
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions{
FileProvider = new
PhysicalFileProvider(#"\\TS3410D618\NickFlicks\Movies"),
RequestPath = new PathString("/Movies"),
EnableDirectoryBrowsing = false
});
This works perfectly when I run it with IIS express(debugging), however, when I deploy it to my local IIS on the same machine the application fails to start with the error:
Application startup exception: System.ArgumentException: The directory name
\\TS3410D618\NickFlicks\MoviePosters\ is invalid.
This is a valid UNC path and can be reached from everywhere I have tried on my windows 10 machine. As I mentioned everything is on the same machine except for the shared drive.
As a side note I can mount a virtual drive from within IIS sucessfully, so IIS can access the fileshare without any issues.
I'm not sure what I am doing wrong here, any help would be greatly appreciated.
I could never get this to work because I couldn't find a way to create an Active Directory with Windows 10 or to modify the user permissions for the UNC file share for the user used by IIS. Basically, I just installed Ubuntu server with Nginx as the reverse proxy and everything worked fine once I set up the permissions properly, which was very straight forward.

application redirect to IIS root folder during upload excel file in asp.net

I am facing issue in my web application which is made in asp.net. I have host the application on IIS server.and I am going to upload excel file to port data in database from application.when i uploading from locally from server itself its working fine but i when i try to uploading using public ip or outside then my application redirect to IIS root folder can any one tell me whats is the issue.
this could be an permission issue on your web.config, file and folder permission or maybe a missing reference to correct folder.
Web.config
What is the authentication ? Do you allow Anonymous access. I understand that you can upload fine if you open the side from localhost from the server itself. Did you try to open the side with public ip from the server itself ?
Application Pool in IIS
Look for the "Identify" configuration and set this to "ApplicationPoolIdentify"
File Permission
Look for the folder and ensure that the "IIS_IUSERS" got write permissions
Code Try to sepecify the correct path using the server.MapPath()
Dim myPath As String = Server.MapPath("foldername") & "\" & "filename.csv"

Can't find .bin file generated by app inside docker container

I have a c#/mono app running inside a docker container, using the very cool Dokku.
The app allows for token authentication and saves the keys used to validate incoming tokens inside the folder /keyChain/keyChain.bin in the root folder of the app. The app generates this folder and file by itself when it soes not exists, usually after a fresh deploy and the first auth requests comes in.
I need to mount the keyChain folder as a docker volume so the keychain gets persisted between deploys and not all users sessions are expired, forcing all users to sign in again.
However, although the app runs fine and auth works, the keyChain folder and it's contents are nowhere to be found inside the app folder, inside the Docker container, so there is nothing to mount...
Running dokku run app-name ls /app shows the contents of the app but not the keyChain folder. Running dokku run app-name find /app -type f -name "keyChain.bin" to just look for the file that way yields no results either.
I am using the NancyFx framework for the app with the Nancy.Authentication.Token package.
I am not fully sure if this is a docker/dokku related issue, a c#/mono issue or NancyFx/Nancy.Authentication.Token issue. Remember, the apps WORKS, so the keyChain.bin file must be inside the container somewhere...
Any insight is appreciated :-).

IIS 8( windows server 2012) cannot run batch file

Im hosting an asp.net application in IIS8 on windows server 2012.
this application suppose to execute an batch file.
it works perfect if i execute the application with visual studio in debug mode. but when i upload it to the IIS the application cant execute the batch file.
i tried to change the batch file with exe file. same problem.
the batch file suppose to execute from the application(WCF application) that in the IIS:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = #"C:\path";
proc.StartInfo.FileName = #"C:\path\executer.bat";
proc.Start();
proc.Close();
please help,
Most likely the problem you are experiencing is due to permissions. You need to check what account the IIS Application Pool that your app is running under is configured for and whether that account has rights to run your batch file or the commands within that batch file.
When running in Visual Studio you're likely running IIS Express as the interactive user.
When running under IIS by default you're running under ApplicationPoolUser identity which has no rights to execute code, has no file access or anything else. Unless you've explicitly set a different account with appropriate file access rights to the batch file, and rights to execute the commands inside of the batch file, you won't be able to run the batch file from within IIS. To change that change the Application Pool user identity to a user that does have rights to both read and execute the batch file on disk and has any rights required to run what's executing in the batch file.
Make sure any folder or file accessed by your application have permissions granted to the AppPoolUser account. It is also important to check your applications resource folders too. For example, if you are writing logs, make sure you give the appPool user account enough permission to write to that file.
Your code is working fine . May be there is some issue in batch file i faced before .My batch file is
start "" BATCHLOG.exe
where BATCHLOG.exe is the executable
Some times batchfile named only
BATCHLOG.exe
does not work correctly on windows scheduler so may be in that case of yours

Elastic Beanstalk not running any commands when deploying asp.net using "Publish to AWS" Visual Studio command

I just registered at AWS services (micro, free) and trying to deploy asp.net mvc 4 application.
Everything is working fine, web application deployed and running (the web part, database, etc), but when i am trying to write to filesystem (for example, when i`m saving a file), i'm gettig an exception
System.UnauthorizedAccessException: Access to the path
'C:\inetpub\wwwroot.logs\xxx.log
The folder "C:\inetpub\wwwroot\.logs" exists.
I tried to grant a permission to folder to DefaultAppPool using commands.
I have following web project structure:
WebHost
.ebextensions
aws.config
aws.cofig has following content:
container_commands:
01-logscreate:
command: "mkdir C:\inetpub\wwwroot\.logs > create-logs.log"
cwd: "C:/inetpub/wwwroot/.ebextensions"
01-logspermission:
command: "icacls \"C:/inetpub/wwwroot/.logs\" /grant DefaultAppPool:(OI) (CI) > p-logs.log"
cwd: "C:/inetpub/wwwroot/.ebextensions"
When i accessing the file, i resolving the full path using
HostingEnvironment.MapPath("~/.logs/xxx.log")
But when i trying to write to this file, a get an exception.
Relative path of '.ebextensions' in deployment package is
Content\D_C.git\udsmonitoring.app\WebHost\obj\Debug\Package\PackageTmp\.ebextensions
Thanx in advance
The app pool that gets deployed runs with limited permissions. It doesn't have access to write to that directory.
Further, you really shouldn't need to be writing logs to the instance file system itself. These instances are transient. If you need a logging solution, consider using CloudWatch or LogEntries. Both probably fit into your existing code base with only a config file change required. In general, if what you're doing ends up with you needing to remote into the EC2 instance to do or see something, consider taking an alternate approach.
Finally, if you really do have a need to write to that directory from within the context of your app, you can add a command in your ebextensions that uses appcmd to set the app pool identity to one with greater privileges...or have a command that sets the directory permissions explicitly.

Categories

Resources