1

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

Sreerejith S S
  • 258
  • 5
  • 18
  • I got the answer by checking out http://stackoverflow.com/questions/6674203/ldap-retrieve-a-list-of-all-attributes-values that helps me – Sreerejith S S Dec 24 '15 at 07:02

3 Answers3

1

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.

Community
  • 1
  • 1
C0d1ngJammer
  • 550
  • 1
  • 6
  • 21
0

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..

Community
  • 1
  • 1
Sreerejith S S
  • 258
  • 5
  • 18
0

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

How to get User Data from the Active Directory

user3141985
  • 1,395
  • 4
  • 17
  • 36