Creating directories in medium trust environment?

守給你的承諾、 提交于 2019-12-20 06:24:54

问题


I've got an ASP.NET Web Application running in a medium trust environment with a shared hosting provider. The following code causes a SecurityException to be thrown:

private void TestButton_Click(object sender, EventArgs e)
{
    string directory = Server.MapPath("~/MyFolder/") + "_TestDirectory";
    if (!Directory.Exists(directory))
        Directory.CreateDirectory(directory);
}

The full text of the error is:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, DirectorySecurity dirSecurity)
   at System.IO.Directory.CreateDirectory(String path, DirectorySecurity directorySecurity)
   at ASP.testcreatedirectory_aspx.TestButton_Click(Object sender, EventArgs e)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The Zone of the assembly that failed was:
MyComputer

The folder where the subfolder is being created has full permissions, so I don't think that's the problem. This looks like something to do with running in a medium trust environment.

Is it normal for medium trust environments to disallow the creation of new directories (via the Directory.Create method), and/or is there any workaround for this?


回答1:


As long as the path you are trying to access is under the Virtual Directory your application is in, you should be able to access it in Medium Trust. Are you sure your application identity has folder create permission?

http://msdn.microsoft.com/en-us/library/aa302425#c09618429_015


Edit: I might have read the doc above wrong. See this link as well, it appears you only have Read, Write, Append, and PathDiscovery permissions :(

FileIOPermission is restricted. This means you can only access files in your application's virtual directory hierarchy. Your application is granted Read, Write, Append, and PathDiscovery permissions for your application's virtual directory hierarchy.

http://msdn.microsoft.com/en-us/library/ff648344.aspx



来源:https://stackoverflow.com/questions/6387888/creating-directories-in-medium-trust-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!