【问题标题】:System.Security.Permissions.FileIOPermission Error When Trying to Upload a File Based on Trust Levels尝试根据信任级别上传文件时出现 System.Security.Permissions.FileIOPermission 错误
【发布时间】: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&lt;location path="myAppName" allowOverride="false"&gt;添加到我的配置文件中。现在应用程序可以在 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


【解决方案1】:

嗯,我已经找到了解决方案,而且非常简单:(就我而言,我使用了

string filename = Path.GetFileName(new FileInfo(file.FileName).Name);

获取文件名,这是不必要的。我不知道为什么我这样做了,但是

string filename = file.FileName

足以获取上传文件的文件名。其余代码相同,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>

【讨论】:

    【解决方案2】:

    这是中等信任度的常见问题。

    您应该能够在配置中使用身份模拟来解决它:

    <system.web>
    <identity impersonate="true" userName="HOSTING\myUserName" password="myPassword"/>
    </system.web>
    

    更多细节在这里: https://forums.asp.net/t/1052417.aspx

    获得有效登录的确切过程取决于您的托管解决方案,您没有提供任何详细信息。

    您还可以在 SO 上找到一些资源:例如 Unable to upload a file in medium trust,但这个特定问题是指另一个似乎因某种原因被删除的问题。

    【讨论】:

    • 首先,感谢您的回答。但到目前为止我没有帮助我。我的托管服务提供商是IHS。所以解决方案可能与 GoDaddy 不同。我尝试过模仿,但也没有帮助。我遇到Could not create Windows user token from the credentials specified in the config file. Error from the operating system 'The username or password is incorrect. 实际上每个人都可以在我的应用程序中上传文件,所以我认为问题不在于身份验证。我也无法从我的计算机读取(和写入)文件。我怎样才能给自己这个权限?
    • 我也不确定&lt;trust level="Medium" originUrl=""/&gt; 是否有效。你能确认它是否正确吗?
    • 你确定正确吗?这是有关该主题的另一页:practicalhost.zendesk.com/hc/en-us/articles/…您也可以直接向您的托管服务提供商索取指导。
    • 我使用&lt;identity impersonate="true" userName="mywebsite.com\ftpUserID" password="ftpPassword"/&gt; 作为您的链接建议,但也没有帮助。我还问了我的托管服务提供商,但他们说我应该对我的软件进行更改,更改 web.config 文件将无济于事单独
    • “更改软件”是 Ops 人员的常见回答;)。他们建议改变什么?即使在中等信任环境中,Server.MapPath 对我来说似乎也不错,而且您的代码很简单……您是否阅读过其他链接,例如 stackoverflow.com/questions/3126337/… ?此外,检查代码的哪一行引发了异常对我们来说会很有趣。而且我不知道您的“originUrl”是否有用,我猜没有但不确定。
    猜你喜欢
    • 2011-06-02
    • 2019-04-17
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    相关资源
    最近更新 更多