using System; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BeAUserSync.AD { class SHUAD { DirectoryEntry di; string ldapServer = ""; string userName = ""; string password = ""; string AuthType = ""; string filter = ""; public string ErrorMessage = ""; public bool Connect_to_Server() { ldapServer = Properties.Settings.Default.LDAPServer; userName = Properties.Settings.Default.UserName; password = Properties.Settings.Default.Password; AuthType = Properties.Settings.Default.AutheticationType; Console.WriteLine(ldapServer); Console.WriteLine(userName); try { AuthenticationTypes authenticationType = (AuthenticationTypes)Enum.Parse(typeof(AuthenticationTypes), this.AuthType); //di = new DirectoryEntry(ldapServer, userName, password,atype); if (userName !="") { if (password == "") { Console.Write("Passwort für User:" + userName); password = Console.ReadLine(); }; //di = new DirectoryEntry(ldapServer, userName, password,atype); di = new DirectoryEntry(ldapServer, userName, password, authenticationType); return true; } else { di = new DirectoryEntry(ldapServer); return false; } } catch (Exception ex) { ErrorMessage = ex.Message; return false; } } public string get_mail_from_user(string userid) { try { string email = ""; DirectorySearcher searcher = new DirectorySearcher(di); searcher.Filter = "(" + Properties.Settings.Default.LoginAttribute + "=" + userid + ")"; //searcher.Filter = "(uid="+userid+")"; //searcher.Filter = "(SAMAccountName = " + userid + ")"; searcher.PropertiesToLoad.Add(Properties.Settings.Default.emailattribute); SearchResult rc = searcher.FindOne(); try { email = rc.Properties[Properties.Settings.Default.emailattribute][0].ToString(); return email; } catch (Exception ex) { //Console.WriteLine(ex.Message); return ""; } } catch (Exception ex) { Console.WriteLine(ex.Message); return ""; } } } }