【问题标题】:SecurityException when creating new FileInfo on Rackspace在 Rackspace 上创建新 FileInfo 时出现 SecurityException
【发布时间】:2010-05-20 17:23:42
【问题描述】:

将文件上传到 Rackspace Cloud Files 时出现以下异常:

安全异常
说明:应用程序试图执行未执行的操作 安全策略允许的。到 授予此应用程序所需的权限 权限请联系您的系统 管理员或更改 应用程序的信任级别 配置文件。

异常详情:System.Security.SecurityException: 请求类型的许可 'System.Security.Permissions.FileIOPermission, mscorlib,版本=2.0.0.0, 文化=中立, PublicKeyToken=b77a5c561934e089' 失败

这似乎只发生在这个文件上。

这种情况发生在我检查唯一文件名的方法中,但我似乎无法弄清楚原因。

    private string GetUniqueStorageItemName(string storageItemName)
    {
        int count = 0;
        string Name = "";


        if (cloudConnection.GetContainerItemList(Container).Contains(storageItemName))
        {
            System.IO.FileInfo f = new System.IO.FileInfo(storageItemName); // error on this line
            if (!string.IsNullOrEmpty(f.Extension))
            {
                Name = f.Name.Substring(0, f.Name.LastIndexOf('.'));
            }
            else
            {
                Name = f.Name;
            }

            while (cloudConnection.GetContainerItemList(Container).Contains(storageItemName))
            {
                count++;
                storageItemName = Name + count.ToString() + f.Extension;
            }
        }

        return storageItemName;
    }

【问题讨论】:

  • 好的,有时只是发布问题有助于阐明问题。该文件已经存在于服务器上......我想这里的问题是“为什么我不能使用 FileInfo”或者“我真的需要使用它吗 - 我想我可以绕过它”

标签: c# asp.net rackspace cloudfiles


【解决方案1】:

您的应用程序似乎在中等信任或更低的信任下运行。看看这篇关于信任级别的博客文章以及您如何可能能够更改它们...这取决于 Rackspace 的配置方式:

ASP.NET trust levels demystified

【讨论】:

  • 谢谢,试过了,由于继承的设置,我无法更改信任级别。
  • @senloe - 这就是我认为会发生的事情。在托管环境中,您通常无法直接访问文件系统(因为它通常是共享环境)。
【解决方案2】:

解决使用 FileInfo 的问题。将代码更改为以下。这是最好的解决方案吗?

            if(storageItemName.Contains('.'))
            {
                Name = storageItemName.Substring(0, storageItemName.LastIndexOf('.'));
                Ext = storageItemName.Substring(storageItemName.LastIndexOf('.'), storageItemName.Length - storageItemName.LastIndexOf('.'));
            }
            else
                Name = storageItemName;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多