public string LoginToAD(string Username, string Password)
    {
        //DataSet ds = new DataSet();
        string username = "";
        int InputLength = 1;
        if (Username.Length >= InputLength && Password.Length >= InputLength)
        {
            string ServerLDAP = "LDAP://xxx.xxx.xxx.xxx:389/";
            string BaseDN = "dc=xxx,dc=xxx,dc=xxx";
            string Path = ServerLDAP + BaseDN;
            DirectoryEntry de = new DirectoryEntry(Path, Username, Password);
            SearchResultCollection results;
            DirectorySearcher mySearcher = new DirectorySearcher(de);
            mySearcher.Filter = "(&(objectClass=*)(cn=" + Username + "))";
            try
            {
                results = mySearcher.FindAll();
                #region return from LDAP

                if (results.Count > 0)
                {
                    //ถ้าค้นหาใน AD แล้วเจอ
                    foreach (SearchResult sResultSet in mySearcher.FindAll())
                    {
                        DirectoryEntry re = sResultSet.GetDirectoryEntry();
                        username = re.Properties["cn"].Value.ToString();
                        PropertyCollection props = re.Properties;
                        foreach (string propName in props.PropertyNames)
                        {
                            if (re.Properties[propName].Value != null)
                            {
                                Response.Write("<br/>"+propName + " = " + re.Properties[propName].Value.ToString());
                            }
                            else
                            {
                                Response.Write("<br/>" + propName + " = NULL");
                            }
                        }
                        //Session["firstname"] = re.Properties["givenNameThai"].Value;
                        //Session["lastname"] = re.Properties["serNameThai"].Value;
                        //Session["position"] = re.Properties["title"].Value;
                        //Page.RegisterStartupScript("alert", "<script language='javascript'>alert('count 1.')</script>");

                    }
                    //foreach (string property in de.Properties.PropertyNames)
                    //{
                    //    Response.Write("\t"+property+" : "+de.Properties[property][0]);
                    //}

                }
                else
                {
                    //กรณีค้นหาไม่ตรงกับ OU เช่นมี user นี้อยู่บน AD แต่ไม่ตรงกับ Concept ของ BaseDN
                    //Page.RegisterStartupScript("alert", "<script language='javascript'>alert('this account it's not staff in passport.')</script>");

                }
                #endregion

            }
            catch (Exception ex)
            {

            }

        }
        return username;
    }

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.