我正在 Visual Studio 2013 上开发一个 Web 应用程序。在我的应用程序中,用户可以上传图像(保存到计算机的本地文件系统,发布后保存到服务器的文件系统)。我将网站发布到我的主机。但是上传时出现了问题。我联系了支持人员,他们告诉我他们不允许Full Tust,他们允许中等信任级别的应用程序。我在 web.config 中添加了以下行以将应用程序的信任级别设置为中等:
<trust level="Medium" originUrl=""/>
但是当我上传文件尝试时,我遇到了以下错误:
说明:应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请联系您的系统管理员或在配置文件中更改应用程序的信任级别。
异常详细信息:System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”类型的权限失败。
有没有办法给自己中等信任级别的文件操作?我正在寻找解决方案数周,但没有任何东西派上用场。
这是导致问题的代码。
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
请帮忙。
编辑:到目前为止我所做的
我根据我得到的这个链接添加了安全策略配置
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息: System.Web.HttpException:无法读取信任级别“中”的安全策略文件。
错误。我尝试创建自定义安全策略文件并更改$AppDir$ 以外的FileIOPermission内容。但这也无济于事。然后我创建新的 web.config 文件。我复制了 web_mediumtrust.config 的内容。但也没有解决。最后,我删除了安全策略标签及其所有内容。我用
<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:<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>