2012-06-25 18 views
1

我們在特殊的Sharepoint頁面中託管了一個ascx自定義控件(不是Web部件)。此頁面允許用戶將文件上傳到我們的服務器。不幸的是,許可問題阻止Sharepoint將文件保存到網絡位置。在Sharepoint中託管的ASP.NET ascx控件在嘗試保存文件時獲取System.UnauthorizedAccessException

歸因於基於Sharepoint 2007的網站的應用程序池的網絡帳戶具有授予該位置的「修改」和「讀取」訪問權限。

我們使用應用程序池帳戶使用的憑據登錄到其他計算機,並且可以在指定的網絡位置沒有任何問題地創建目錄和文件。

是否有可能Sharepoint試圖使用其他帳戶來保存這些文件,而不是IIS7中的應用程序池設置?

我們得到的錯誤:

消息:訪問路徑 '\蛋白石\ GWL \的圖片\ L36' 被拒絕。

堆棧跟蹤:在System.IO .__ Error.WinIOError(的Int32的errorCode,字符串maybeFullPath)在System.IO.Directory.InternalCreateDirectory(字符串FULLPATH,字符串路徑,DirectorySecurity dirSecurity)在System.IO.Directory.CreateDirectory (字符串路徑,directorySecurity directorySecurity)在ECan.SharePoint.Web.Applications.MyECan_WaterMeterFormDatalogger.SavePhotos()

異常類型: System.UnauthorizedAccessException的

用戶:系統帳號

在文件後面的ascx代碼SavePhotos函數的代碼:

protected void SavePhotos() 
{ 
    string wellNo = WellNo.Value; 
    string epoWaterMeterID = EPO_WaterMeterID.Value; 
    string dirRoot = ConfigurationManager.AppSettings["PhotoDir"]; 
    string map = wellNo.Substring(0, wellNo.IndexOf('/')); 

    int photoSaveCount = 1; 
    foreach (string filePath in Request.Files) 
    { 
     HttpPostedFile file = (HttpPostedFile)Request.Files[filePath]; 
     if (file.InputStream.Length > 0) 
     { 
      try 
      { 
       // Create dir if does not exist 
       string dir = dirRoot + map; 
       if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); 

       // Save file 
       file.SaveAs(dir + @"\" + wellNo.Replace('/', '_') + "-" + epoWaterMeterID.ToString() + "-" + photoSaveCount.ToString() + ".jpg"); 

       photoSaveCount++; 
      } 
      catch (Exception ex) 
      { 
       Logger.Write(ex); 
      } 
     } 
    } 
} 

任何人有任何想法什麼問題可能是什麼?

回答

1

我認爲你必須調用具有提升privildges的SavePhotos。 運行具有提升權限的代碼將執行具有完全控制權限的指定方法,即使用戶不具有完全控制權限也是如此。

見鏈接:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges(v=office.12).aspx

請嘗試下面的代碼:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(SavePhotos); 
    SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups); 
} 
+0

我已經應用了代碼更改,但仍然遇到同樣的問題。感謝您的想法,但將其松鼠,以備將來參考!對測試框的進一步調查似乎指向IIS上應用程序池使用的帳戶,因爲測試帳戶沒有問題。 – Gavin

+0

事實證明,它是上下文Sharepoint正在保存文件。剛開始時沒有捕捉到它,因爲測試環境應該完全反映現場環境。在測試Sharepoint服務器上,該文件正在使用運行IIS應用程序池的相同帳戶進行保存,但在正在使用IUSR的活動框中保存。 – Gavin

0

您是否嘗試設置新創建的目錄或文件夾的權限?您可以通過使用System.Security.AccessControl命名空間中的DirectorySecurity類,特別是該類的SetAccessControl方法來完成此操作。

相關問題