在 Rackspace 上创建新 FileInfo 时出现 SecurityException
将文件上传到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的应用程序正在以“中等信任”或更低的级别运行。看看这篇关于信任级别的博客文章以及如何更改它们......这取决于 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
使用 FileInfo 进行解决。将代码更改为以下内容。这是最好的解决方案吗?
Working around using FileInfo. Changed the code to the following. Is this the best solution?