无法在生产中获得Active Directory的扩展属性。

时间:2016-04-08 03:46:04

标签: c# active-directory ldap-query extended-properties

我使用以下代码获取扩展属性" JobCode"

    [DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
    }
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context)
        : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled)
        : base(context, samAccountName, password, enabled)
    { }

    // Create the "extensionAttribute2" property.    
    [DirectoryProperty("JobCode")]
    public string JobCode
    {
        get
        {
            if (ExtensionGet("JobCode").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("JobCode")[0];
        }
        set { ExtensionSet("JobCode", value); }
    }
}

我可以使用Windows身份验证在本地工作时获取该属性。

        private string GetJobCode(string username)
    {
        string jobCode = String.Empty;
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
        {
            UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx,  username);
            if (inetPerson != null)
            {
                jobCode = inetPerson.JobCode;
                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(inetPerson.JobCode == String.Empty ? "no job code found" : "job code is " + jobCode));
            }
            else
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("JobCode property was not found"));
            }
        }
        return jobCode;
    }

但是这段代码找不到扩展属性" JobCode"在生产中意味着它在部署时。

请帮助

0 个答案:

没有答案