. NET core get AD Domain Information
. NET Core 2.1.4
. NET core is now System.DirectoryServices only for use under Windows platforms.
Reference:
https://github.com/dotnet/standard/pull/444
https://github.com/dotnet/corefx/issues/2089
Privatedictionary<string,string> Authenticateactivedirectory (stringUsernamestringpassword) {Dictionary<string,string> dic =Newdictionary<string,string>(); DirectoryEntry Entry=NewDirectoryEntry (_appconfiguration["Ldap:de"], username, password); Try{DirectorySearcher Search=NewDirectorySearcher (entry); Search. Filter= $"(Samaccountname={username})"; SearchResult result=Search. FindOne (); if(Result! =NULL) {dic. ADD (" State","true"); Dic. ADD ("DisplayName", result. properties["DisplayName"]? [0]. ToString ()); Dic. ADD ("Mail", result. properties["Mail"]? [0]. ToString ()); } } Catch(Exception ex) {dic. ADD (" State","false"); Dic. ADD ("errmsg", ex. Message); } returnDiC;}
Novell.Directory.Ldap
NOVELL.DIRECTORY.LDAP supports the. NET Core2 Linux environment.
Publicdictionary<string,string> Ldapauthenticate (stringUsernamestringpassword) {Dictionary<string,string> dic =Newdictionary<string,string>(); varLdaphost = _appconfiguration["Ldap:host"]; varLdapPort = _appconfiguration.getvalue<int> ("Ldap:port"); varMailsuffix = _appconfiguration["Ldap:mailsuffix"]; varSearchbase = _appconfiguration["Ldap:searchbase"]; varLOGINDN =username; varsAMAccountName =username; if(username. Contains (Mailsuffix)) sAMAccountName= Username. Substring (0, username. IndexOf (Mailsuffix)); ElseLogindn= $"{Username}{mailsuffix}"; varSearchfilter = $"(Samaccountname={samaccountname})"; varAttrs = _appconfiguration["Ldap:attrs"]. Split ('|');Try { varconn =Newldapconnection (); Conn. Connect (Ldaphost, LdapPort); Conn. Bind (logindn, password); varLSC = conn. Search (Searchbase, Ldapconnection.scope_sub, Searchfilter, Attrs,false); while(Lsc.hasmore ()) {Ldapentry NextEntry=NULL; Try{nextentry=Lsc.next (); } Catch(Ldapexception ex) {Logger.debug (ex). ToString (), ex); Continue; } varAttributeSet =Nextentry.getattributeset (); varIenum =Attributeset.getenumerator (); while(Ienum. MoveNext ()) {varattribute =(Ldapattribute) ienum. Current; varAttributeName =attribute. Name.tolower (); varAttributeval =attribute. StringValue; if(attrs. Contains (AttributeName)) {dic. ADD (AttributeName, attributeval); }} dic. ADD (" State","true"); } conn. Disconnect (); } Catch(Exception ex) {dic. ADD (" State","false"); Dic. ADD ("errmsg", ex. Message); Logger.debug (ex. ToString (), ex); } returnDiC;}
The above configuration information is as follows:
"LDAP": { "_comment":"domain account Login configuration", "DE":"ldap://xxx.com", "Host":"xx.xx.xx.xx", "Port":389, "Mailsuffix":"@xxx. com", "Attrs":"DISPLAYNAME|MAIL|SN", "Searchbase":"dc=xxx,dc=com", "userrole":"User" },
. NET core get AD Domain Information