#region get the ad user name and the OU directory based on the current logged-in domain account///<summary>/////////////////////For the domain user name and the OU directory </summary>//<param name= "Searchuser" > Current user name to search </param>//<param name= "Paths" >o UT returns the user's OU directory </param>///<param name= "UserName" >out returns the user's name </param>//<returns> Search Whether a user of the domain exists </returns> public static bool Getadusernameandpaths (string searchuser, out list<string> paths, Out string userName) {bool Isexite = false;//The domain user exists ... DataContextProvider.loger.Info (String. Format (">>>>>>>>>>>>>>>>>>>>>>>>> >> get current domain user information log>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>start>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>≫>>>>>>>>>>>>>>>>> ")); DirectoryEntry entry = null; String name = "Unknown"; List<string> Directorys = null; try {//Read config file ... Connect login domain Server//"ldap://10.2.17.*" Domain server address//login//Password DATACONTEXTP Rovider.loger.Info (String. Format ("Domain service address: {0}, domain account: {1}, domain password {2},,", Confighelper.domainaddress, Confighelper.domainaccount, Confighelper.domainpassword)); Using (Entry=new DirectoryEntry (Confighelper.domainaddress, Confighelper.domainaccount, Confighelper.domainpassword, authenticationtypes.secure)) {if (entry! = NULL) {DirectorySearcher mysearcher = new DirectorySearcher (entry); Mysearcher.filter = ("(& (| ( Objectclass=user) (Objectclass=organizationalunit) (Objectclass=group)) (| ( Cn= "+ Searchuser +") (dn= "+ Searchuser + ") (samaccountname=" + Searchuser + "))); (& (Objectclass=organizationalunit) (ou= Power Technology Department)); SearchResult SearchResult = Mysearcher.findone (); if (SearchResult! = null) {Isexite = true;//the domain user exists ... string path = Searchresult.path; DataContextProvider.loger.Info (String. Format ("Get the oupath>>>>>>>>>>>>>>>>>>>>> of the current login domain account >>>>>>{0} ", path)); string[] Splits = path. Split (new char[] {', '}); for (int i = splits. Count ()-1; I >= 0; i--) {if (Splits[i]. Contains ("ou=")) {if (directorys==null) { Directorys = new list<string> (); } directorys. ADD (Splits[i]. Substring (Splits[i]. IndexOf (' = ') + 1); }} String xing = (string) (searchresult.properties["SN"]. count>0?searchresult.properties["SN"][0]: ""); String ming = (string) (searchresult.properties["givenname"]. count>0?searchresult.properties["givenname"][0]: ""); Name = (xing + ming); DataContextProvider.loger.Info (String. Format ("Get the name >>>>>>> properties of the current login domain account >>>>>name>>>>>>>>> >>>>>>{0} ", name)); }}}} catch (Exception ex) {Dataconte XtProvider.loger.Error ("Get the ad user name and the OU directory where the exception occurred:" + ex.) TostriNg ()); }//finally//{////Release resources//entry. Close (); Entry. Dispose (); } paths = Directorys; UserName = name; return isexite; } #endregion
Get the ad user name and the OU directory based on your current login domain account