2
Reply

How ot add user programminticaly in Active directory using LDAP protocol in C#

    2nd ANS:-public static bool IsAuthenticUser(this UserModel model){string propertyName = "Name";bool isAuthenticated = false;string username = model.UserName;string domain = model.DomainName;string password = model.Password;string ldapServer = "";string ldapBaseDN = "";string ldapFilterAdmin = "";string ldapFilterNormal = "";try{using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapServer + "/" + ldapBaseDN, username, password)){DirectorySearcher ds = new DirectorySearcher(entry);ds.SearchScope = SearchScope.Subtree;ds.Filter = string.Format(ldapFilterAdmin, username);SearchResult result = ds.FindOne();if (result != null){DirectoryEntry personEntry = result.GetDirectoryEntry();if (personEntry.Name != null){model.Name = result.Properties[propertyName][0].ToString();model.UserType = Constants.CONST_ADIMIN_USERTYPE;isAuthenticated = true;}else{isAuthenticated = false;}}if (isAuthenticated == false){ds.Filter = string.Format(ldapFilterNormal, username);result = ds.FindOne();if (result != null){DirectoryEntry personEntry = result.GetDirectoryEntry();if (personEntry.Name != null){model.Name = result.Properties[propertyName][0].ToString();model.UserType = Constants.CONST_NORMAL_USERTYPE;isAuthenticated = true;}else{isAuthenticated = false;}}}}}catch (AuthenticationException ex){ErrorLogUtil.LogMessage("ExtentionMethod", "IsAuthenticUser", ex, Enums.LogType.Error);}return isAuthenticated;}

    public static bool IsAuthenticUserLDAP(this UserModel model){string propertyName = "Name";bool isAuthenticated = false;string username = model.UserName;string domain = model.DomainName;string password = model.Password;DirectoryContext context = new DirectoryContext(DirectoryContextType.Forest, domain, username, password);try{Forest forest = Forest.GetForest(context);GlobalCatalog gc = null;gc = forest.FindGlobalCatalog();if (gc != null){DirectorySearcher searcher = gc.GetDirectorySearcher();searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + username + ")(memberof=CN=PRM_WEB_,OU=Groups,OU=CLS,OU=NA-Organizations,DC=na,DC=wkglobal,DC=com))";foreach (SearchResult result in searcher.FindAll()){if (result != null){DirectoryEntry personEntry = result.GetDirectoryEntry();if (personEntry.Name != null){model.Name = result.Properties[propertyName][0].ToString();model.UserType = Constants.CONST_ADIMIN_USERTYPE;isAuthenticated = true;}else{isAuthenticated = false;}}}if (isAuthenticated == false){searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + username + ")(memberof=CN=PRM_WEB_,OU=Groups,OU=CLS,OU=NA-Organizations,DC=na,DC=wkglobal,DC=com))";foreach (SearchResult result in searcher.FindAll()){if (result != null){DirectoryEntry personEntry = result.GetDirectoryEntry();if (personEntry.Name != null){model.Name = result.Properties[propertyName][0].ToString();model.UserType = Constants.CONST_NORMAL_USERTYPE;isAuthenticated = true;}else{isAuthenticated = false;}}}}}}catch (AuthenticationException ex){ErrorLogUtil.LogMessage("ExtentionMethod", "IsAuthenticUser", ex, Enums.LogType.Error);}return isAuthenticated;}