Access denied
-
I'm running a little code that should write code to a text file within the application directory, however I'm getting the following error: Access to the path c:\inetpub\wwwroot\MapFile\Mapped.txt is denied I'm logged on as Administrator on Windows Server 2008 and running VS 2008 SP1 / .NET 3.5. Any ideas?
Jon
-
I'm running a little code that should write code to a text file within the application directory, however I'm getting the following error: Access to the path c:\inetpub\wwwroot\MapFile\Mapped.txt is denied I'm logged on as Administrator on Windows Server 2008 and running VS 2008 SP1 / .NET 3.5. Any ideas?
Jon
Navigate to that folder, view the properties -> security. Give Users write permissions.
-
I'm running a little code that should write code to a text file within the application directory, however I'm getting the following error: Access to the path c:\inetpub\wwwroot\MapFile\Mapped.txt is denied I'm logged on as Administrator on Windows Server 2008 and running VS 2008 SP1 / .NET 3.5. Any ideas?
Jon
This can also come up if the file has already been opened somewhere else in your code and you've forgotten to close it.
My Blog: This Blog
-
I'm running a little code that should write code to a text file within the application directory, however I'm getting the following error: Access to the path c:\inetpub\wwwroot\MapFile\Mapped.txt is denied I'm logged on as Administrator on Windows Server 2008 and running VS 2008 SP1 / .NET 3.5. Any ideas?
Jon
If this is an ASP.NET application, the accout you're using to run this code under the debugger doesn't matter. It's actually running under the ASPNET account, which IIRC, by default, does not have Write permissions to the application folder. In order to make this work, and I do NOT recommend this, you have to give the ASPNET account Write permissions to the C:\inetpub\www\MapFile folder.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
If this is an ASP.NET application, the accout you're using to run this code under the debugger doesn't matter. It's actually running under the ASPNET account, which IIRC, by default, does not have Write permissions to the application folder. In order to make this work, and I do NOT recommend this, you have to give the ASPNET account Write permissions to the C:\inetpub\www\MapFile folder.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Yeah I thought so, but what is the ASP.net acct used? When checking the users on the OS there were none other than the Administrator and another one created by sql server. This is not a production environment, so I'm not worried about security, but it's good to know.
Jon
-
I'm running a little code that should write code to a text file within the application directory, however I'm getting the following error: Access to the path c:\inetpub\wwwroot\MapFile\Mapped.txt is denied I'm logged on as Administrator on Windows Server 2008 and running VS 2008 SP1 / .NET 3.5. Any ideas?
Jon
This method should work for you. public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { try { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = fInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType)); // Set the new access settings. fInfo.SetAccessControl(fSecurity); } catch (Exception exp) { Console.WriteLine(exp.Message.ToString()); Console.Read(); } } You can use it with something like this: FileWritePermission.AddFileSecurity(path, "everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow); Cheers
-
Yeah I thought so, but what is the ASP.net acct used? When checking the users on the OS there were none other than the Administrator and another one created by sql server. This is not a production environment, so I'm not worried about security, but it's good to know.
Jon
jon_80 wrote:
but what is the ASP.net acct used?
Depends on the server being used. If it's the development web server that comes with Visual Studio, it's "ASPNET". If it's IIS 6.0 and below, it'll probably be something like "IUSR_machineName". For IIS 7.0 and above, I think this name got changed to just "IUSR".
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
This method should work for you. public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { try { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = fInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType)); // Set the new access settings. fInfo.SetAccessControl(fSecurity); } catch (Exception exp) { Console.WriteLine(exp.Message.ToString()); Console.Read(); } } You can use it with something like this: FileWritePermission.AddFileSecurity(path, "everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow); Cheers
Cute code, but an account (and core running under that account) cannot grant more permissions to itself, nor anyone else, than it already has.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008