Monday, September 10, 2018

Asp core AD

using System.DirectoryServices;

            DirectoryEntry ldap = new DirectoryEntry("LDAP://10.102.172.250:389/OU=VPGTUsers,DC=vpgt,DC=local");
            DirectorySearcher lookFor = new DirectorySearcher(ldap);

            string user = User.Identity.Name; user = user.Substring(user.LastIndexOf("\\") + 1);
            lookFor.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", user);
            lookFor.SearchScope = SearchScope.Subtree;
            SearchResultCollection results = lookFor.FindAll();
            SearchResult result = (results.Count == 1) ? results[0] : null;
            //foreach (string property in result.Properties.PropertyNames)
            //    Debug.WriteLine("\t{0} : {1} ", property, result.Properties[property][0]);

            ModelState.AddModelError("", (string)result.Properties["department"][0]);
            ModelState.AddModelError("", (string)result.Properties["title"][0]);

No comments:

Post a Comment