0
votes

i am using the following code to get Extended property "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); }
    }
}

i can get the property when working locally using windows authentication.

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

but this code cannot find the extended property "JobCode" when in production means when it is in deployed.

kindly help

Do you logs on your prod? Do you know if you're getting any results back on this line jobCode = inetPerson.JobCode or you're getting errors.smr5
I just get empty string at that lineRaas Masood
And you have confirmed that particular user you're searching for has JobCode set in AD?smr5
Yes i can see the string result in visual studio.Raas Masood
For few users if JobCode is not there inetPerson obj is null. But for the users i am check they have JobCode. Just returns empty in production.Raas Masood