【发布时间】:2017-02-01 08:09:11
【问题描述】:
我正在 Visual Studio 2013 上开发 Web 应用程序。在我的应用程序中,用户可以上传图像(保存到计算机的本地文件系统,发布后保存到服务器的文件系统)。我将网站发布到我的主机。但是上传时出现了问题。我联系了支持人员,他们告诉我他们不允许 Full Trust,他们允许 Medium 应用信任级别。我在 web.config 中添加了以下行以将应用程序的信任级别设置为中等:
<trust level="Medium" originUrl=""/>
但是当我上传文件尝试时,我遇到了以下错误:
说明:应用程序试图执行安全策略不允许的操作。授予此申请 所需权限请联系您的系统管理员或更改 配置文件中应用程序的信任级别。
异常详情:System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission”类型的权限, mscorlib,版本=4.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089' 失败。
有没有办法让自己fileiopermission处于中等信任级别?我正在寻找解决方案数周,但没有任何东西派上用场。
这是导致问题的代码。
foreach (var file in uploadImages.PostedFiles)
{
//this line causes the problem
string filename = Path.GetFileName(new FileInfo(file.FileName).Name);
string[] extension = filename.Split('.');
string path = Server.MapPath("~/fortunePictures/" + randomString(16) + "." + extension.Last().ToString());
file.SaveAs(path);
DateTime now = DateTime.Now;
string date = (now.ToString("u"));
date = date.Substring(0,date.Length-1);
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
insertImage(file, path, date, img, userID, fortuneID);
}
这是堆栈跟踪:
[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +34
System.Security.CodeAccessPermission.Demand() +46
System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath) +157
System.IO.FileInfo.Init(String fileName, Boolean checkHost) +42
System.IO.FileInfo..ctor(String fileName) +46
Fal_Sitesi.kahve.btnUpload_Click(Object sender, EventArgs e) in c:\Users\Ömer\Documents\Visual Studio 2013\Projects\Fal Sitesi\Fal Sitesi\kahve.aspx.cs:84
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9717914
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6720
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +245
System.Web.UI.Page.ProcessRequest() +72
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +22
System.Web.UI.Page.ProcessRequest(HttpContext context) +58
ASP.kahve_aspx.ProcessRequest(HttpContext context) in App_Web_n3utt0vk.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
请帮忙。
编辑:到目前为止我做了什么
我根据这个link添加了安全策略配置我得到了
说明:在执行过程中发生未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详情: System.Web.HttpException:无法读取 信任级别“中”的安全策略文件。
错误。我尝试创建自定义安全策略文件并更改 $AppDir$ 以外的 FileIOPermission 内容。但这也无济于事。然后我创建新的 web.config 文件。我复制了 web_mediumtrust.config 的内容。但也没有解决。最后,我删除了 security policy 标签及其所有内容。我用了
<identity impersonate="true" userName="mywebsite.com\ftpUserID" password="ftpPassword"/>
以授权连接服务器。但我无法建立联系。 (我不知道为什么,用同样的数据我可以建立ftp连接。)
结果没有解决我的问题,我很想解决它。这是我的 web.config。
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation targetFramework="4.5" debug="true"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0"/>
<customErrors mode="Off" defaultRedirect="index.aspx"/>
<trust level="Medium" originUrl=""/>
</system.web>
</configuration>
我得到 System.Security.SecurityException 这个配置。
编辑2:我根据这个link将<location path="myAppName" allowOverride="false">添加到我的配置文件中。现在应用程序可以在 localhost 上正常运行。但是发布的网站仍然抛出错误。这是我的 web.config 文件的最新版本:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<location path="myAppName" allowOverride="false">
<system.web>
<compilation targetFramework="4.5" debug="true"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0"/>
<customErrors mode="Off" defaultRedirect="index.aspx"/>
<trust level="Medium" originUrl=""/>
</system.web>
</location>
</configuration>
【问题讨论】:
-
从上面的错误消息中,它清楚地表明您需要完全信任权限才能运行您的应用程序。请让您的托管服务提供商为您提供完全许可。
-
感谢您的回复,但他们只允许中等信任权限。正如我在回答中提到的那样,我已经解决了这个问题。
-
很高兴听到这个消息。
标签: c# asp.net medium-trust full-trust