在中等信任环境中创建目录?
我有一个 ASP.NET Web 应用程序在具有共享托管提供商的中等信任环境中运行。以下代码导致抛出 SecurityException:
private void TestButton_Click(object sender, EventArgs e)
{
string directory = Server.MapPath("~/MyFolder/") + "_TestDirectory";
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
}
错误的全文是:
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
正在创建子文件夹的文件夹具有完全权限,所以我认为这不是问题。这看起来与在中等信任环境中运行有关。
中等信任环境不允许创建新目录(通过 Directory.Create 方法)是否正常,和/或是否有任何解决方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您尝试访问的路径位于应用程序所在的虚拟目录下,您就应该能够在中等信任中访问它。您确定您的应用程序身份具有文件夹创建权限吗?
http://msdn.microsoft.com/en-us/library/aa302425#c09618429_015编辑
:我可能读错了上面的文档。另请参阅此链接,看来您只有读取、写入、追加和 PathDiscovery 权限:(
http://msdn.microsoft.com/en-us/library/ff648344.aspx
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 :(
http://msdn.microsoft.com/en-us/library/ff648344.aspx