【问题标题】:Can't get PrincipalContext to work in SharePoint 2010 with Claims Based Authentication无法使用基于声明的身份验证在 SharePoint 2010 中使用 PrincipalContext
【发布时间】:2013-10-24 08:21:26
【问题描述】:

我使用的是 SharePoint 2010,但我似乎无法让此代码在我们的生产环境中返回任何内容。服务器设置为基于声明的身份验证。

private string GetADName(string userID)
{
    try
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

        // define a "query-by-example" principal - here, we search for a UserPrincipal 
        // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
        UserPrincipal qbeUser = new UserPrincipal(ctx);
        qbeUser.SamAccountName = userID;

        // create your principal searcher passing in the QBE principal    
        PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

        // find all matches
        foreach (var found in srch.FindAll())
        {
            return found.Name;
        }
    }
    catch (Exception ex)
    {
        this.lblErrors.Text = ex.Message + "<br />\r\n" + ex.StackTrace;
    }
    return "";
}

【问题讨论】:

  • 这段代码是否在您的暂存环境中工作?如果你传入“*”而不是用户 ID,你会得到结果吗?

标签: c# sharepoint-2010 active-directory


【解决方案1】:

我不得不使用 HostingEnvironment.Impersonate()

    private string GetADName(string userID)
    {
        try
        {
            using (HostingEnvironment.Impersonate())
            {

                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                UserPrincipal qbeUser = new UserPrincipal(ctx);

                qbeUser.SamAccountName = userID.ToLower();

                PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

                foreach (var found in srch.FindAll())
                {
                    if (found.SamAccountName.ToLower() == userID.ToLower())
                    {
                        return found.Name;
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
        return "";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2012-12-24
    • 2012-03-26
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多