public ValidationResult AuthenticateUser(UserDetails user) { ValidationResult validationResult = null; try { LdapConnection lcon = new LdapConnection(new LdapDirectoryIdentifier(_adServerAddress, _ldapPortNumber)); NetworkCredential nc = new NetworkCredential(user.UserName, user.Password, Environment.UserDomainName); lcon.Credential = nc; lcon.AuthType = AuthType.Negotiate; lcon.Bind(nc); validationResult = new ValidationResult(true, false, null); } catch (LdapException e) { //tbd - investigate other possible ldap exceptions //if (e.Message == "The supplied credential is invalid.") if (e.ErrorCode.Equals(LDAPError_InvalidCredentials)) { validationResult = new ValidationResult(false, true, e.Message); } else { //implement logging and exception email handling here. validationResult = new ValidationResult(false, true, "A system error occured, please contact system administrator and/or check system logs."); } } catch (Exception e) { validationResult = new ValidationResult(false, true, "A system error occured, please contact system administrator and/or check system logs."); //add new fields for error logging var errorLoggingWSClient = new ErrorLoggingServiceClient(); errorLoggingWSClient.LogApplicationError(new ApplicationErrorRequest() { ApplicationName = "KingstonWharvesWS.ADAuthentication" }); } return validationResult; }
public ValidationResult AuthenticateUserAndGetGroupMemberships(UserDetails user) { var validationResult = AuthenticateUser(user); if (!validationResult.IsAuthenticated) { return validationResult; } else { //get groups as well validationResult.ListOfADGroups = GetGroups(user); return validationResult; } }
private ICollection<string> GetGroups(UserDetails user) { var listOfADGroups = new List<string>(); using (var pc = new PrincipalContext(ContextType.Domain, "KWLJM", "DC=KWLJM,DC=NET")) { var user2 = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, Environment.UserDomainName + "\\" + user.UserName); if (user2 != null) { PrincipalSearchResult<Principal> results = user2.GetAuthorizationGroups(); listOfADGroups = results.Select(c => c.Name.ToLower()).ToList(); } var testBoolx = pc.ValidateCredentials("ChrisW", "FridayPass*23444123"); var testBool = pc.ValidateCredentials("ChrisW", "FridayPass*123"); } return listOfADGroups; }