SP2010中安装的基本身份验证WCF服务,具有声明身份验证

时间:2010-12-14 16:09:38

标签: sharepoint-2010 basic-authentication claims-based-identity

此配置适用于经典模式下的SP2010或SP2007。

我们有一个在Sharepoint网站下作为应用程序安装的WCF服务。此应用程序使用基本身份验

我收到了 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...

1 个答案:

答案 0 :(得分:0)

嗯...迟到总比没有好。我今天遇到了同样的问题。当您在_Layouts文件夹中发布.ASPX页面时会出现问题,然后,在使用Forms或Claims auth时,使该自定义页面成为会话中的第一个页面(之前没有记住登录)。默认情况下不会触发SharePoint身份验证(即使您从LayoutsPageBase类继承)。如果您导航到其他某个SharePoint页面(例如_Layouts / 15 / Settings.aspx)然后返回,则会填充CurrentUser 。我必须使用Reflector来更好地了解正在进行,以及如何解决它。简短的回答是,一旦你意识到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;
}