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; 
}