1

此配置適用於傳統模式或SP2007中的SP2010。基本身份驗證安裝在SP2010中的WCF服務使用聲明身份驗證

我們有一個WCF服務,作爲S​​harepoint網站下的應用程序安裝。該應用程序使用基本認證。

我得到一個UnauthorizedAccessException。 異常消息是'訪問被拒絕。 (來自HRESULT的異常:0x80070005(E_ACCESSDENIED))'。

在調試器中,我注意到在SPWeb對象上,CurrentUser屬性爲null。

我需要做些什麼來允許此用戶通過基本身份驗證才能夠讀取共享點列表?

using (SPSite siteCollection = new SPSite(url)) 

     { 
      using (SPWeb rootWeb = siteCollection.OpenWeb()) 
      { 
       DataTable news = ReadNews(rootWeb, (uint)sizeNumber); 

/// continues... 

回答

0

好吧......比從未更好的遲到。我今天遇到了同樣的問題。當您在_Layouts文件夾中發佈.ASPX頁面時,出現這些問題,然後在使用表單或聲明身份驗證時,使該自定義頁面在會話中首次點擊(沒有先前記住的登錄)。 SharePoint身份驗證默認情況下不會觸發(即使您從LayoutsPageBase類繼承)。如果您導航到某個其他SharePoint頁面(例如_Layouts/15/Settings.aspx)然後回來,則當前用戶填寫。我不得不使用反射器來更好地瞭解正在發生的事情,並且如何解決它。簡短的回答是,一旦你意識到CurrentUser == NULL,則需要添加這行代碼:

Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException()); 

在我而言,這段代碼生成一個挑戰/響應瀏覽器,這在我以前登錄並緊跟在這行代碼之後,CurrentUser對象被正確填寫。這裏是我的整個功能最終看起來像這樣:

public static bool isAdminAuthorized() 
{ 
    Microsoft.SharePoint.SPContext oContext ; 
    Microsoft.SharePoint.SPWeb oWeb ; 
    Microsoft.SharePoint.SPUser oUser ; 
    try 
    { 
     oContext = Microsoft.SharePoint.SPContext.Current; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint Context!"); } 
    try 
    { 
     oWeb = oContext.Web; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint web!"); } 
    try 
    { 
     oUser = oWeb.CurrentUser; 
    } 
    catch { throw new Exception("Can't obtain Sharepoint current user!"); } 
    if (oUser == null) 
    { 
     Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(new UnauthorizedAccessException()); 
     oUser = oWeb.CurrentUser; 
    } 
    foreach (Microsoft.SharePoint.SPGroup oGroup in oUser.Groups) 
    { 
     if (oGroup.Name.ToUpper().Contains("OWNER")) 
     { 
      return true; 
     } 
    } 
    return false; 
}