簡體   English   中英

嘗試基於信任級別上載文件時出現System.Security.Permissions.FileIOPermission錯誤

[英]System.Security.Permissions.FileIOPermission Error When Trying to Upload a File Based on Trust Levels

我正在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”的權限失敗。

有沒有一種方法來給自己的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

請幫忙。

編輯:到目前為止我做了什么

我根據獲得的此鏈接添加了安全策略配置

說明:執行當前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">到我的配置文件中。 現在,該應用程序可以在本地主機上正常工作。 但是,已發布的網站仍然會引發錯誤。 這是我的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>

好吧,我找到了解決方案,它很簡單:(就我而言,

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>

中信任度是一個常見問題。

您應該能夠在配置中使用身份模擬來解決它:

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

此處有更多詳細信息: https : //forums.asp.net/t/1052417.aspx

進行有效登錄的確切過程取決於您的托管解決方案,在此不提供任何詳細信息。

您還可以在SO上找到一些資源:例如, 無法以中等信任度上傳文件,但是此特定問題涉及另一個似乎已出於某種原因被刪除的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM