Sreerejith S S
Sreerejith S S

Reputation: 258

How to get country of a user from active directory form Membership.GetUser(uname)

I am authenticated using active directory . I am successfully authenticate using

Membership.ValidateUser(login.UserName, login.Password)

method and getting user details from active directory using Membership.GetUser(login.UserName) but I cant get the country name how can i get the country name or code from AD anyone please help

Upvotes: 1

Views: 2667

Answers (3)

user3141985
user3141985

Reputation: 1385

Although it is very late, but the tutorial at following link can really help

How to get User Data from the Active Directory

Upvotes: 0

Sreerejith S S
Sreerejith S S

Reputation: 258

I got the answer from LDAP - Retrieve a list of all attributes/values?,

  PrincipalContext ctx = new PrincipalContext(
                                        ContextType.Domain,
                                        ConfigurationManager.AppSettings["ADDomainName"],
                                        ConfigurationManager.AppSettings["ADContainer"],
                                        ConfigurationManager.AppSettings["ADUserName"],
                                        ConfigurationManager.AppSettings["ADPassword"]);
                UserPrincipal users = UserPrincipal.FindByIdentity(ctx, user.UserName);
                DirectoryEntry entry = users.GetUnderlyingObject() as DirectoryEntry;
                PropertyCollection props = entry.Properties;

                if (entry.Properties["countryCode"].Value != null)
                {
                    user.CountryCode = entry.Properties["countryCode"].Value.ToString();
                }

It may helps anyone..

Upvotes: 0

C0d1ngJammer
C0d1ngJammer

Reputation: 550

The country is not a Property by default in MembershipUser , unless you manually specified them in your profile provider.

U have got to use the System.Web.Profile.ProfileBase class . Here a greate class from @Sky Sanders which also uses the Membership class

public class UserProfile : ProfileBase
{
    public static UserProfile GetUserProfile(string username)
    {
        return Create(username) as UserProfile;
    }
    public static UserProfile GetUserProfile()
    {
        return Create(Membership.GetUser().UserName) as UserProfile;
    }


    [SettingsAllowAnonymous(false)]
    public string CountryCode
    {
        get { return base["countryCode"] as string; }
        set { base["countryCode"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string Description
    {
        get { return base["Description"] as string; }
        set { base["Description"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string Location
    {
        get { return base["Location"] as string; }
        set { base["Location"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string FavoriteMovie
    {
        get { return base["FavoriteMovie"] as string; }
        set { base["FavoriteMovie"] = value; }
    }
}

Here are some helpful links How to assign Profile values? How can i use Profilebase class?

Hope it helps.

Upvotes: 1

Related Questions