C# Can';t让PrincipalContext在SharePoint 2010中使用基于声明的身份验证

C# Can';t让PrincipalContext在SharePoint 2010中使用基于声明的身份验证,c#,sharepoint-2010,active-directory,C#,Sharepoint 2010,Active Directory,我正在使用SharePoint 2010,但我似乎无法让此代码在我们的生产环境中返回任何内容。服务器设置为基于声明的身份验证 private string GetADName(string userID) { try { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); // define a "query-by-example" principal - here,

我正在使用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 "";
}
私有字符串GetADName(字符串userID)
{
尝试
{
PrincipalContext ctx=新PrincipalContext(ContextType.Domain);
//定义一个“示例查询”主体-在这里,我们搜索一个UserPrincipal
//名字(吉文纳姆)是“布鲁斯”,姓氏是“米勒”
UserPrincipal qbeUser=新的UserPrincipal(ctx);
qbeUser.SamAccountName=userID;
//创建传递QBE主体的主体搜索者
PrincipalSearcher srch=新PrincipalSearcher(qbeUser);
//查找所有匹配项
foreach(在srch.FindAll()中找到的变量)
{
返回找到的名称;
}
}
捕获(例外情况除外)
{
this.lblErrors.Text=ex.Message+“
\r\n”+ex.StackTrace; } 返回“”; }
我不得不使用HostingEnvironment.Impersonate()


我不得不使用HostingEnvironment.Impersonate()


我不得不使用HostingEnvironment.Impersonate()


我不得不使用HostingEnvironment.Impersonate()


这段代码在您的登台环境中工作吗?如果传入“*”而不是userID,是否会返回结果?此代码是否在您的登台环境中工作?如果传入“*”而不是userID,是否会返回结果?此代码是否在您的登台环境中工作?如果传入“*”而不是userID,是否会返回结果?此代码是否在您的登台环境中工作?如果传入“*”而不是userID,是否会返回结果?
    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 "";
    }