在 Rackspace 上创建新 FileInfo 时出现 SecurityException

发布于 2024-09-02 16:28:22 字数 1265 浏览 11 评论 0原文

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

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

异常详细信息:System.Security.SecurityException: 请求类型许可 '系统.安全.权限.FileIOPermission, mscorlib,版本=2.0.0.0, 文化=中立, 公钥令牌=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;
    }

I'm getting the following exception when uploading a file to Rackspace Cloud Files:

Security Exception
Description: The application attempted to perform an operation not
allowed by the security policy. To
grant this application the required
permission please contact your system
administrator or change the
application's trust level in the
configuration file.

Exception Details: System.Security.SecurityException:
Request for the permission of type
'System.Security.Permissions.FileIOPermission,
mscorlib, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089'
failed

It seems to only happen with this file.

It is happening in a method where I check for a unique file name and I can't seem to figure out why.

    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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

累赘 2024-09-09 16:28:22

看起来您的应用程序正在以“中等信任”或更低的级别运行。看看这篇关于信任级别的博客文章以及如何更改它们......这取决于 Rackspace 如何配置:

ASP.NET 信任级别揭秘

It looks like your application is running at Medium Trust or lower. Take a look at this blog post about Trust Levels and how you might be able to change them...it depends on how Rackspace configures things:

ASP.NET trust levels demystified

嘿哥们儿 2024-09-09 16:28:22

使用 FileInfo 进行解决。将代码更改为以下内容。这是最好的解决方案吗?

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

Working around using FileInfo. Changed the code to the following. Is this the best solution?

            if(storageItemName.Contains('.'))
            {
                Name = storageItemName.Substring(0, storageItemName.LastIndexOf('.'));
                Ext = storageItemName.Substring(storageItemName.LastIndexOf('.'), storageItemName.Length - storageItemName.LastIndexOf('.'));
            }
            else
                Name = storageItemName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文