Home
Manage Your Code
Snippet: Validate A User's Active Directory Credentials (pre-Windows XP) (C#)
Title: Validate A User's Active Directory Credentials (pre-Windows XP) Language: C#
Description: How to validate a user's credentials in Windows 2000. For XP, use the code snippet "Validate A User's Active Directory Credentials". Views: 209
Author: Kevin Shuma Date Added: 11/28/2007
Copy Code  
1        /// <summary>

2        /// Validate a user's credentials against active directory.

3        /// 

4        /// Only use this method for PRE-XP operating systems.

5        /// </summary>

6        /// <param name="DomainName">string.  The user's domain name.  Example "AMERICAS"</param>

7        /// <param name="UserName">string.  User name. Example "kevin_shuma"</param>

8        /// <param name="Password">string.  The user's password.</param>

9        /// <returns>bool.  True = authentication success, False = authentication failure.</returns>

10        public static ValidateUserCredentialsReturnCodes
11            ValidateUserCredentialsPreXP(string DomainName, string UserName, string Password)
12        {
13            try
14            {
15                DirectoryEntry User = new DirectoryEntry("LDAP://" + DomainName, UserName, Password, AuthenticationTypes.Secure);
16
17                // the following assignment will raise an error if the login information is incorrect

18                try
19                {
20                    UserName = User.Name;
21                }
22                catch (Exception AuthEx)
23                {
24                    // make sure we got an authentication error

25                    if (AuthEx.Message.IndexOf("Logon failure") == -1) // not an authentication error

26                        throw AuthEx; // rethrow the error to the general exception handler

27                    else
28                    {
29                        // return an error indicating bad user name or password

30                        return ValidateUserCredentialsReturnCodes.ERROR_BAD_USERNAME_OR_PASSWORD;
31                    }
32                }
33
34                // everything passed okay, authentication was successful

35                return ValidateUserCredentialsReturnCodes.SUCCESS;
36            }
37            catch (Exception ex)
38            {
39                //Logging.LogException(MethodBase.GetCurrentMethod(), ref ex);

40                return ValidateUserCredentialsReturnCodes.UNKNOWN_ERROR;
41            }
42        }