How set the user's password

Mar 21, 2014 at 8:57 AM
Hi everyone,

Context : VS2013, dotnet 4.5

I'm currently working on a app with the magic LinqToLdap library to manage contacts and users from AD.
The code below is used into an app console in order to create users with the “class map” technic. Actually, it’s work well but the password. Like you can see in the code below I have had to write the “SetPassword” method to set the user’s password. So, I’m wondering if someone can drive me on how set the user’s password properly with the LinqToLdap.
public class UserMap : ClassMap<UserDto>
   {
       public override IClassMap PerformMapping(string namingContext = null, string objectCategory = null, bool includeObjectCategory = true, IEnumerable<string> objectClasses = null, bool includeObjectClasses = true)
       {
           if (namingContext == null)
           {
               namingContext = DataAccess.Dal.DalAd.Config.SITE_NAMINGCONTEXT;
           }
           NamingContext(namingContext);
           ObjectClass("user");
           DistinguishedName(x => x.DistinguishedName);
           Map(x => x.Guid).Named("objectguid").StoreGenerated();
           Map(x => x.WhenCreated).StoreGenerated();
           Map(x => x.WhenChanged).StoreGenerated();
           Map(x => x.CommonName).ReadOnly().Named("cn");
           Map(x => x.DisplayName).Named("displayName");
           Map(x => x.Description);
           Map(x => x.Password).Named("userPassword");
           Map(x => x.AccountControl).Named("userAccountControl");
           Map(x => x.AccountName).Named("sAMAccountName");
           return this;
       }
   }


public void Insert(string numSite, UserDto item)
        {
            try
            {
                UserDto result = null;
                var factory = new LdapConfiguration();
                factory.ConfigureFactory(DataAccess.Dal.DalAd.Config.CONFIGUREFACTORYSTRING).AuthenticateAs(new NetworkCredential { Domain = DataAccess.Dal.DalAd.Config.LDAPSTRING, UserName = DataAccess.Dal.DalAd.Config.LDAPUSER, Password = DataAccess.Dal.DalAd.Config.LDAPPASSWORD });
#else
                factory.AddMapping(new UserMap(), "OU=" + numSite + "," + DataAccess.Dal.DalAd.Config.SITE_NAMINGCONTEXT);
                using (var context = new DirectoryContext(factory))
                {
                    
                    item.DistinguishedName = "CN=" + item.CommonName + "," + "OU=" + numSite + "," + DataAccess.Dal.DalAd.Config.SITE_NAMINGCONTEXT;
                    item.AccountControl = DataAccess.Dal.DalAd.Config.LDAPUSERACCOUNTCONTROL;
                    result = context.AddAndGet(item);
                    item.Guid = result.Guid;
                    item.DistinguishedName = result.DistinguishedName;
                    item.WhenCreated = result.WhenCreated;
                    item.WhenChanged = result.WhenChanged;
                }
                SetPassword(item);
            }
            catch (Exception ex)
            {
                LoggerManager.Log(ex.Message, LoggerManager.TypeLog.Ad, LoggerManager.CriticityLog.Error);
                throw ex;
            }
        }

 private void SetPassword(UserDto item)
        {
            DirectoryEntry dirEntry = new DirectoryEntry(DataAccess.Dal.DalAd.Config.CONFIGUREFACTORYSTRING, DataAccess.Dal.DalAd.Config.LDAPUSER, DataAccess.Dal.DalAd.Config.LDAPPASSWORD);
            dirEntry.Path = "LDAP://" + item.DistinguishedName;
            dirEntry.AuthenticationType = AuthenticationTypes.Secure;
 
            DirectorySearcher searcher = new DirectorySearcher(dirEntry);
            searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + item.AccountName + "))";
            SearchResult result = searcher.FindOne();
 
            if (result != null)
            {
                DirectoryEntry userEntry = result.GetDirectoryEntry();
                userEntry.Invoke("SetPassword", new object[] { item.Password });
                userEntry.CommitChanges();
                userEntry.Close();
            }
        }
thx in advance for your help,

Cedric
Coordinator
Mar 26, 2014 at 5:01 AM
Hi Cedric,

I actually had a blog post about passwords here. I hope that helps.