Get the ad user name and the OU directory based on your current login domain account

Source: Internet
Author: User
Tags domain server in domain

  #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>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>&Gt;>>>>>>>>>>>>>>>>> "));            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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.