| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 |  
        /// <summary>
        /// Try to connect Username with password
        /// </summary>
        /// <param name="username">Username to test</param>
        /// <param name="passwd">Username's password</param>
        /// <param name="domain">Domain to connect</param>
        /// <returns>True: Username/Password OK; False: Authentication error</returns>
        public bool IsAuthenticated(string username, string passwd, string domain)
        {
            try
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, passwd, AuthenticationTypes.Secure);
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(objectClass=user)";
                search.SearchScope = SearchScope.Subtree;
                SearchResult result = search.FindOne();
 
                foreach (ResultPropertyValueCollection var in result.Properties.Values)
                {
                    foreach (object var2 in var)
                    {
                        Console.WriteLine(var2.ToString());
                    }
 
                }
 
                return (result != null);
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }
        } | 
Partager