0
System.UnauthorizedAccessException: Access to the path 'C:\Inetpub\wwwroot\Spicee    Webservice\App_Data\Employee.xml' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32   rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at ResturantService.InsertDatabaseXML(String servername, String dbusername, String dbpassword, String dbname, String port)

I get the above error when i try to write a xml file. The code is working fine when i run it from VS2008.

I know this is something related to permission set for iis folder. But i don't want to do the permission setting manual because our plan is to distribute the application via website deployment package user will not have an idea to set the permission manually.

Also when i try to set permission to the folder using the below code for the iisuser.

 Directory.CreateDirectory(sPathName);
        DirectoryInfo info = new DirectoryInfo(sPathName);
        DirectorySecurity security = info.GetAccessControl();

        security.AddAccessRule(new FileSystemAccessRule(@"TAG1\IUSR_TAG1", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
        security.AddAccessRule(new FileSystemAccessRule(@"TAG1\IUSR_TAG1", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));

        info.SetAccessControl(security); 

i get this error.

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
at System.IO.Directory.SetAccessControl(String path, DirectorySecurity directorySecurity)
at System.IO.DirectoryInfo.SetAccessControl(DirectorySecurity directorySecurity)
at ResturantService.InsertDatabaseXML(String servername, String dbusername, String dbpassword, String dbname, String port)

Please help me in solving this issue.

Thanks in advance.

user941105
  • 31
  • 1
  • 2

1 Answers1

0

Well you need to change whatever you use to deploy your site to give write permissions to whatever account is being used to execute this code, or you need to put it somewhere else, or you need to set up an another account that can do this, so you don't hand out the permission to everyone and his dog.

It's no different to how a system works manually. If you log on as a low rights user, you can't just assign yourself higher permissions. You have to be authorised at a higher level in order to do that. It's like a UAC hit, or SUDO.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39