2012-12-24 49 views
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) 

当我尝试写入xml文件时,出现上述错误。代码工作正常,当我从VS2008运行它。在iis中写入xml时访问被拒绝路径

我知道这是与为iis文件夹设置的权限有关的东西。但我不想做权限设置手册,因为我们的计划是通过网站部署包分发应用程序,用户不会有任何想法手动设置权限。

此外,当我尝试使用下面的代码为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); 

我得到这个错误。

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) 

请帮我解决这个问题。

在此先感谢。

回答

0

那么你需要改变任何你用来部署你的网站给予写权限的任何帐户正在用来执行此代码,或者你需要把它放在别的地方,或者你需要建立一个另外的帐户,可以做到这一点,所以你不会向每个人和他的狗分发权限。

这与系统如何手动工作没有什么不同。如果您以低权限用户身份登录,则不能仅为自己分配更高的权限。为了做到这一点,您必须获得更高级别的授权。这就像UAC命中,或SUDO。