在中等信任环境中创建目录?

发布于 2024-11-15 20:26:43 字数 1309 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-22 20:26:43

只要您尝试访问的路径位于应用程序所在的虚拟目录下,您就应该能够在中等信任中访问它。您确定您的应用程序身份具有文件夹创建权限吗?

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


:我可能读错了上面的文档。另请参阅此链接,看来您只有读取、写入、追加和 PathDiscovery 权限:(

FileIOPermission 受到限制。这
意味着您只能访问以下位置的文件
您的应用程序的虚拟目录
等级制度。您的申请已获批准
读取、写入、追加和路径发现
您的应用程序的权限
虚拟目录层次结构。

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 :(

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文