0

我正在使用以下代码获取扩展属性“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”,这意味着在部署时。

好心的帮助

4

0 回答 0