Read Write Permissions on IIS - asp.net

I have create a directory uploads in the Content directory and inside the uploads dir i create a directory at the time of creation of user account. This setup works fine on the development setup but when i publish the site on my local IIS i get a access denied error.
Access to the path 'C:\inetpub\wwwroot\Content\uploads/d1bed3b7-f9e1-486a-953a-37abc7b99dad' is denied.
I have a shared hosting environment, the question i want to ask is, will i be able to set the permissions on the shared hosting environment?
What are the alternatives?
EDIT
here is code(tnx to #Marko) for creating the directory for the user
string UserGUID = Guid.NewGuid().ToString();
string path = HostingEnvironment.MapPath(#"~/Content/uploads");
Directory.CreateDirectory(path+"/" + UserGUID);
EDIT
after changing the code the error message has improved
Access to the path 'C:\inetpub\wwwroot\Content\uploads\ab569312-c487-4a08-bf49-99f7c69303f6' is denied.

There's a problem with the path, you have the wrong slash /
C:\inetpub\wwwroot\Content\uploads--------->/<----------d1bed3b7-f9e1-486a-953a-37abc7b99dad'
As long as the \uploads\ path has writable permissions any folders/files you create within that folder should inherit permissions from the parent.
EDIT
I'm not 100% sure why it works in cassini but change your code to this (it uses Path.Combine) instead of adding a slash manually (which is actually the wrong slash).
string UserGUID = Guid.NewGuid().ToString();
string path = HostingEnvironment.MapPath(#"~/Content/uploads");
string userPath = Path.Combine(path, UserGuid);
Directory.CreateDirectory(userPath);
If all of this fails, contact your hosting provider to set writable permissions to the /uploads/ folder or use the Administration panel if you have one.

Related

HostingEnvironment.VirtualPathProvider.FileExists returns false on existing file

I'm trying to read a file in a virtual path, using the following code
HostingEnvironment.VirtualPathProvider.FileExists(_SiteMapFileName)
_SiteMapFileName has something like "~/Content/en-US/MainMenu.sitemap" and it is always returning false.
But if I access the URL of that virtual path (http://www.local.mysite.com/Content/en-US/MainMenu.sitemap), the file displays correctly in the browser
Content is a virtual directory under my website, pointing to a shared folder on a different location. If I access that location on Windows Explorer I can see the files correctly
I've set the site and the virtual directory to connect as my network account.
I used the process monitor tool to see if there's any problem and it says that w3wp.exe is trying to create the file \path\to\shared\location\Content\en-US\MainMenu.sitemap but it is getting ACCESS DENIED which is fine since it is a read-only location
Does anybody now how to solve this issue?
I found out the reason. The user set to run the app pool tied to the application needs to have permissions on the file is trying to read, not only the website and the virtual directory. :/
when i am working with vDir i use Server.MapPath
Does that not work for you?
if(System.IO.File.Exists(Server.MapPath("Relative Path to vDir")))
{
//do something with the file
}

ASP - Permission denied

before I start, I'm a PHP programmer, not an ASP programmer, but at my job, they use ASP and I don't have acces to the server, but I can contact the person in charge.
I'm working on an ASP function for work and I alway hit this "Permission denied" error. I made some search and I found that the account need permission to run the script. It's maybe just me, but I don't understand that ASP check the user before run a script, I mean, it's a public website, obviously they will not have acces if I don't.
Can I change the file so anyone can run it ?
EDIT :
My script is trying to edit a file on the server
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath("test.txt"),8,true)
f.WriteLine("This text will be added to the end of file")
f.Close
set f=Nothing
set fs=Nothing
Thanks
The account that IIS uses (probably iusr) needs to have read/write permissions on the target folder. I would recommend using a temp folder (anything other than the website's root folder)
By default, the account will have no permissions. That's simply basic security. You need to have whoever administers that machine give permissions on an appropriate location.

Some or all identity references could not be translated while trying to create directory via asp.net mvc web application

I should be able to create a default directory inside project's root directory when the application_start method(inside global.asax)is called for my asp.net web application.
As IIS users dont have full rights to webroot directory(for directory create) i get access denied error message while trying to create the directory.Also IIS users wont be given full access to the root as per client.I am planning to implement like below to meet the expectations of client.Please let me know if there is any better way out for than this approach?
Approach 1:
a.Create a new user account(windows) in the server.
b.Keep the user account name in config file.
c.Read that account from the config file and create directory with that user account using Directory Security object.
Code:
DirectorySecurity dacl = new DirectorySecurity();
string useraccountForDirectoryCreate = System.Net.Dns.GetHostName() + "\\" + "testaccount";
dacl.AddAccessRule(new FileSystemAccessRule(useraccountForDirectoryCreate,
FileSystemRights.FullControl,
InheritanceFlags.
ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
Directory.CreateDirectory(imageDirectory, dacl);
The above thing worked locally in IIS server(Haven't tested on client's IIS server)
My concern with this:I think it wont be a feasible to ask the client to create a user account to be
created in the server for just creating the single directory.Also in future he might require to give access to more user for directory create.It wont be feasible to
keep on creating the windows account.
Apporach 2:
a)Create a IIS user account having full access to web root for directory create.(is this a better way out,it is same as giving the IIS user full access to web root isn't it.?Please clarify.)
b)Keep the account info in web config file.
c)Read the account info from web config file and create the directory with permisson with the above piece of code.
Problem with this approach:Is i get the following error.
Some or all identity references could not be translated
when the following statement is invoked.
dacl.AddAccessRule(new FileSystemAccessRule(useraccountForDirectoryCreate,
FileSystemRights.FullControl,
InheritanceFlags.
ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
It looks like i am not able to add the access rule for this newly created user.Why is it so?
I have googled enough for the above error message and tried to understand but i am not able to resolve the error.I am not able to understand the error message at all.Please help with the solution for this problem.Its high time to deliver.

Downloading file using webclient results in "Access to the path denied is denied"

i have button, on a click of which i want to download the file on the local pc, i am using webclient.downloadfile(), but i am getting the below error:
Access to the path 'C:\Windows\SysWOW64\inetsrv\ms-banner.gif' is denied.
i am using below code to download file:
WebClient client = new WebClient();
client.DownloadFile(new Uri("http://www.contoso.com/library/homepage/images/ms-banner.gif"), "ms-banner.gif");
i dont understand why its fetching the file from local server, as i have already stated the remote uri
It's fetching the file from the remote server but trying to save it in the current directory because you have only specified a relative filename as second argument: "ms-banner.gif". And it seems that the account you are running your application under doesn't have permission to write to the current working directory which happens to be C:\Windows\SysWOW64\inetsrv.
So you have basically 2 possibilities:
Modify the account you are running your application under and grant it permissions to write to this directory
Specify another location (as an absolute path) to save the file to where the account you are running your application under has write permissions.
contoso.com redirects to microsoft.com... the path you have there in the URL does not exist and you won't be able to download it. I'm not sure why it's trying to go to your local machine, but have you tried some other image on some other website? Like http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif for example?

Access to the path '\\Image0\Abc' is denied

var directory = new DirectoryInfo("\\image0\Abc");
if (!directory.Exists)
{
directory.Create();
}
When I user above ASP.NET code which UNC path, I found the following problem:
Access to the path '\Image0\Abc' is denied.
How can I solve? Regards.
Add a Network Service account to that folder if it is located outside your web application folder. If it is, there might be a problem in the path that you are accessing.

Resources