Sharepoint 为扩展类使用FindByIdentity方法的目录服务

Sharepoint 为扩展类使用FindByIdentity方法的目录服务,sharepoint,active-directory,Sharepoint,Active Directory,从最近几天开始,我一直在使用目录服务 使用UserPrincipal对象,我尝试从当前广告用户获取电子邮件和公司字段。电子邮件不是问题,因为默认情况下它是公开的。不过,公司是另一回事 我在这里发现了一篇解释如何做到这一点的帖子: 尽管如此,我对这种方法有一个不幸的问题。这篇文章展示了如何在扩展类中创建FindByIdentity方法,但是为了实现这一点,您必须将search type设置为您的扩展类类型,这使得result无法找到我的特定用户的条目。如果我在FindByIdentityWithT

从最近几天开始,我一直在使用目录服务

使用UserPrincipal对象,我尝试从当前广告用户获取电子邮件和公司字段。电子邮件不是问题,因为默认情况下它是公开的。不过,公司是另一回事

我在这里发现了一篇解释如何做到这一点的帖子:

尽管如此,我对这种方法有一个不幸的问题。这篇文章展示了如何在扩展类中创建FindByIdentity方法,但是为了实现这一点,您必须将search type设置为您的扩展类类型,这使得result无法找到我的特定用户的条目。如果我在FindByIdentityWithType中将搜索类型设置为UserPrincipal,它确实会找到我的广告用户,但正如您所料,我得到了一个转换错误

所以我的问题很简单,在一个扩展类中,有没有已知的通过身份来查找的方法

以下是我的扩展类,以供参考:

[DirectoryObjectClass("group")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalClassExtensions : System.DirectoryServices.AccountManagement.UserPrincipal
{
    PrincipalContext context;

    public UserPrincipalClassExtensions(PrincipalContext context)
        : base(context)
    { this.context = context; }

    public UserPrincipalClassExtensions(PrincipalContext context, string samAccountName, string Password,bool enabled)
        : base(context, samAccountName, Password, enabled)
    {

    }

    [DirectoryProperty("company")]
    public string Company
    {
        get
        {
            if (ExtensionGet("company").Length != 1)
                return null;

            return (string)ExtensionGet("company")[0];

        }
        set { this.ExtensionSet("company", value); }
    }

    // Implement the overloaded search method FindByIdentity.
    public static new UserPrincipalClassExtensions FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalClassExtensions)FindByIdentityWithType(context, typeof(UserPrincipalClassExtensions), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalClassExtensions FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalClassExtensions)FindByIdentityWithType(context, typeof(UserPrincipalClassExtensions), identityType, identityValue);
    }
}
这是一个返回null的调用:

    SPUser user = SPContext.Current.Web.CurrentUser;
        using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, getUserDomain(user)))
        {
            UserPrincipalClassExtensions UserInfos = UserPrincipalClassExtensions.FindByIdentity(principalContext,IdentityType.SamAccountName,user.Name);

        }
这是一个返回UserPrincipal但不包含company字段值的调用:

    SPUser user = SPContext.Current.Web.CurrentUser;
        using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, getUserDomain(user)))
        {
            UserPrincipal UserInfos = UserPrincipal.FindByIdentity(principalContext,IdentityType.SamAccountName,user.Name);

        }
如果你需要任何进一步的信息,让我知道

提前谢谢

编辑

在IL Spy中再搜索一点并反编译de DLL后,我注意到我的代码有一个问题:

[DirectoryObjectClass("Person")]
[DirectoryRdnPrefix("CN")]
DirectoryObjectClass必须是“Person”

希望这对其他人有帮助