System.Security.Permissions.FileIOPermission when accessing folder outside web site location
Environment: Windows 2008 R2 x64 [IIS7.5]
From my asp.net application, I am trying to access a folder that is not under web application folder itself. It is something like DirectoryInfo("D:\MySecretFolder") I get thrown the following error.
'System.Security.Permissions.File开发者_Python百科IOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed
Application pool is using NetworkService account as its identity and I have given that account full rights on that folder. Is there anything else I am missing.
Thanks
If you are trying to access any files through the system's IIS, you have to give the IIS user accounts access to the file as well, not just the app pool. I am not certain if these are the same accounts for all versions of IIS, but I know IIS 7 uses these accounts:
IUSR and IIS_IUSRS
This is, of course, assuming you are running it around in IIS.
Accessing physical file outside the directory is just as accessing any other file from any other application. The key thing is to understand who are You when You try to gain access. If You are using the IIS (which I think is the case) the Your app is running in an App Pool. App Pool is identified to the system by a specific user account. You can check it using the IIS console (probably default user who is a member of IIS_WPG).
When You try to access a directory that is outside the WebApp the worker process of IIS running in contect of a particular user must be able to access the desired file/dir. You can either give user the permission to access this particualr permision (via filesystem for example), IMPERSONATE a user that has a permission to access this to this resource for this particular demand (via impersonation api and Principal related classess).
One more thing to read is Code access security. I do not know If You use it, or even aware of it, but it's good to know the facilities that the framework provides
http://msdn.microsoft.com/en-us/library/dd984947.aspx
精彩评论