无法获取PrincipalContext在SharePoint 2010的工作与基于声明的身份验证 [英] Can't get PrincipalContext to work in SharePoint 2010 with Claims Based Authentication

查看:173
本文介绍了无法获取PrincipalContext在SharePoint 2010的工作与基于声明的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用SharePoint 2010,我似乎无法得到这个code在我们的生产环境任何回报。将服务器设置为基于声明的身份验证。

 私人字符串GetADName(字符串用户ID)
{
    尝试
    {
        PrincipalContext CTX =新PrincipalContext(ContextType.Domain);

        //定义一个查询通过例如委托 - 在这里,我们搜索UserPrincipal
        //并与米勒头名布鲁斯的(GivenName上)和姓氏(姓氏)
        UserPrincipal qbeUser =新UserPrincipal(CTX);
        qbeUser.SamAccountName =用户ID;

        //创建你的本金搜索传入QBE校长
        PrincipalSearcher SRCH =新PrincipalSearcher(qbeUser);

        //找到所有匹配
        的foreach(VAR在srch.FindAll发现())
        {
            返回found.Name;
        }
    }
    赶上(例外前)
    {
        this.lblErrors.Text = ex.Message +< BR /> \ r \ N+ ex.StackTrace;
    }
    返回 ;
}
 

解决方案

我不得不使用HostingEnvironment.Impersonate()

 私人字符串GetADName(字符串用户ID)
    {
        尝试
        {
            使用(HostingEnvironment.Impersonate())
            {

                PrincipalContext CTX =新PrincipalContext(ContextType.Domain);

                UserPrincipal qbeUser =新UserPrincipal(CTX);

                qbeUser.SamAccountName = userID.ToLower();

                PrincipalSearcher SRCH =新PrincipalSearcher(qbeUser);

                的foreach(VAR在srch.FindAll发现())
                {
                    如果(found.SamAccountName.ToLower()== userID.ToLower())
                    {
                        返回found.Name;
                    }
                }
            }
        }
        赶上(例外前)
        {
        }
        返回 ;
    }
 

I am using SharePoint 2010, and I can't seem to get this code to return anything in our production environment. The server is set up for Claims Based Authentication.

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

解决方案

I had to use 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 "";
    }

这篇关于无法获取PrincipalContext在SharePoint 2010的工作与基于声明的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆