.net cannot read pem file after publish to IIS - c#

I have an MVC Web Application which reads a .pem file for encyption. In IIS Express, I copied the .pem file on IIS Express folder, and I could get it like
RSA rsa = RSA.FromPublicKey(BIO.File("./RSAKeys/TestPublic.pem", "r"));
When I publish the project on IIS the code above gives me the errors:
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
I copied the RSAKeys folder both on the bin folder and the root folder of the project.
I could not get over this error. What can I do?
Thanks.

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
RSA rsa = RSA.FromPublicKey(BIO.File(System.Web.HttpContext.Current.Server.MapPath("~/RSAKeys/TestPublic.pem"), "r"));

Related

Azure App Service - Give file as input to application published app service azure

I have published a web application on App service. Web Application is working correctly on Local run. In a web application, I am taking thumbprint, Certificate file path, and certificate password as input from the user. Certificate details are input correctly into Azure App service published Web application but getting the error " The system can't found the specified path".
Here I am passing local machine contained certificate path from C Drive or D drive.
Please guide how to resolve error.
Please check below image
You can create your path or file OAuth2.0_AzureAD_A in the scm site, under the path D:\home\site. Like below.
We can operate on the path at this level and below. The others should be unchangeable. .

How to access a file from IIS web service hosted by ServiceHost

In my IIS application I open a file located in the wwwroot directory that way:
File.ReadAllText("ConfigFile.json");
IISExpress tries to open the file in C:\Program Files (x86)\IIS Express\ConfigFile.json
I thought the wwwroot directory was the working directory but apparently it's not the case.
Log4net log files are written relatively to the working directory, and configuration manager files also. So I don't understand why opening a file with System.IO.File I have C:\Program Files (x86)\IIS Express as the working directory.
What's the best solution for that problem ? I suppose I don't have to touch the Current defined working directory.
Ok, the solution that works in a IIS WCF Service, hosted by a ServiceHost class is:
AppDomain.CurrentDomain.BaseDirectory + "/ConfigFile.json"
It gives the full absolute path that is valid in my IIS Express environment and in the deployed IIS environment.
You will need to inject IHostingEnvironment into your class to have access to the ApplicationBasePath property value
Suppose IHostingEnvironment type is env then you can use
File.ReadAllText(env.WebRootPath + "/ConfigFile.json");
like you have a function named Read then you can use
public void Read(IHostingEnvironment env)
{
File.ReadAllText(env.WebRootPath + "/ConfigFile.json");
}

decrypt web.config file for specific project on a local machine

I have a project that is running on the server. I want to modify it on a local machine, but connection strings in the web.config file are encrypted using regiis. To decrypt it I tried this article:
http://ryaremchuk.blogspot.com/2012/11/encrypting-and-decrypting-webconfig.html
I copied the project to a folder on the hard desk on my local machine; so its path is C:\project1.
In the prompt interface I reached: C:\windows\Microsoft.NET\Framework\v4.0.30319. Then I used this command with no luck:
aspnet_regiis -pd "connectionStrings" -app "C:\project1"
Is my command wrong? Should the project be in a different path?

How to find my exe is running in local or network shared folder

How to construct root path from registry editor when execute program in web server,while executing in local path i am getting as ""C:\Users\Desktop\SPHelper.exe" "%1"" but in web server i am getting in this format ""webserver\Dot net upload\SPHelper.exe" "%1".
How to construct root path in web server in complete format? for example("\\webserver\Dot net upload\SPHelper.exe""%1")
Hi We can use this to find the Location
System.Reflection.Assembly.GetExecutingAssembly().Location

security issue IIS7.5 / IIS APPPOOL\user not authorized but has full control?

It seems I have a strange issue with security:
I have a website with the following folders:
inetpub\wwwroot
inetpub\wwwroot\readyfordownload
The IIS APPPOOL\Classic user has full access to this 'readyfordownload' folder.
Now I have a console APP that creates a zipfile in the readyfordownload folder. This is done from a c# classlib. Strangely enough, the IIS APPOOL cannot access this file, even though it has full control over the folder. Also, the classlib first creates an xlsx file that is later added to the zip. The APPPOOL user does have access to the xlsx file.
If I run the same function in the C# classlib from a code behind in the website, the same zipfile is created and the IIS APPPOOL user CAN access the file....
Any ideas?
zip is created like this (not the actual code, but it is the same)
http://dotnetzip.codeplex.com/
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("test.xlsx");
zip.Save("MyZipFile.zip");
}
OS is windows 2008 R2 web server
ZIP library is Dotnetzip (Ionic)
Update: I am most interested in why the ZIPfile does not get the rights and the xlsx file does....
Have you tried setting the FileAccessSecurity explicitly? Maybe the files are not inheriting the ACL from the directory.
the apppool user can access the xlsx file because your console creates it directly under readyfordownload folder.
the zip file on the other hand is first created in a temp folder and then copied to your folder. This means that the file permissions are wrongly set on the file.
Make sure IIS_IUSR and DefaultAppPool users have access on your wwwroot.
As scottm suggested change your console code to give permissions to the IUSR and DefaultAppPool users on the zip file. Your code should read like:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("test.xlsx");
zip.Save("MyZipFile.zip");
var accessControl = File.GetAccessControl("MyZipFile.zip");
var fileSystemAccessRule = new FileSystemAccessRule(
#"BUILTIN\IIS_IUSRS",
FileSystemRights.Read | FileSystemRights.ReadAndExecute,
AccessControlType.Allow);
var fileSystemAccessRule2 = new FileSystemAccessRule(
#"IIS AppPool\DefaultAppPool",
FileSystemRights.Read | FileSystemRights.ReadAndExecute,
AccessControlType.Allow);
accessControl.AddAccessRule(fileSystemAccessRule);
accessControl.AddAccessRule(fileSystemAccessRule2);
File.SetAccessControl(path, accessControl);
}
Check Windows EventLog for related errors. For detailed info use ProcessMonitor, so you can see if there is a problem with permissions.
Configure the security of the folder using “advanced securty setting property page”. (Select properties--> security). Also note that the application pool can impersonate the user so that the application may not be serving the request with the identity of the app pool. By default impersonation may not work. You have to set it explicitly in the web config. E.g. <identity impersonate="true" /> or <identity impersonate="true" userName="domain\user" password="password" />
Sriwantha Sri Aravinda

Categories

Resources