public void AddServer(LdapDirectoryIdentifier identifier, int maxConnections, int protocolVersion = 3, bool ssl = false, double? timeout = null, NetworkCredential credentials = null, AuthType? authType = null) { var serverName = identifier.Servers[0]; var factory = new LdapConnectionFactory(serverName); if (credentials != null) factory.AuthenticateAs(credentials); if (authType.HasValue) factory.AuthenticateBy(authType.Value); if (timeout.HasValue) factory.ConnectionTimeoutIn(timeout.Value); factory.ProtocolVersion(protocolVersion); if (identifier.FullyQualifiedDnsHostName) factory.ServerNameIsFullyQualified(); if (identifier.Connectionless) factory.UseUdp(); if (ssl) factory.UseSsl(); factory.UsePort(identifier.PortNumber); _servers[serverName] = new ServerPoolMemberConnectionFactory(serverName, factory, maxConnections); }
public void GetLdapConnection() { var ldapConfigRepository = new LdapConfigRepository(); ldapConfigRepository.BasicLdapConfig(null, LDAPAdminMode.Anonymous, "127.0.0.1:636", "test", AuthType.Basic, LoggerType.None, ""); ldapConfigRepository.AdditionalLdapConfig(secureSocketLayer, transportSocketLayer, clientCertificate, @"LDAPCert/Terena-chain.pem", "test", "test", connectionTimeout); var ldapConnection = LdapConnectionFactory.GetLdapConnection(ldapConfigRepository); Assert.IsInstanceOfType(ldapConnection, typeof(LdapConnection)); Assert.IsTrue(ldapConnection.ClientCertificates.Count == 1); Assert.AreEqual(ldapConnection.Timeout, connectionTimeout); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ protected void StandardConnect(NetworkCredential credential) { if (LdapParameterChecker.ParametersIsNullOrEmpty(new [] { credential.UserName })) { throw new InvalidCredentialException("Username cannot be null or empty"); } if (LdapParameterChecker.ParametersIsNullOrEmpty(new [] { credential.Password })) { throw new InvalidCredentialException("Password cannot be null or empty"); } _ldapConnection = LdapConnectionFactory.GetLdapConnection(_configRepository); _ldapConnection.Bind(credential); }
protected override LdapState ConnectAdmin() { var adminCredential = new NetworkCredential(_configRepository.GetAdminUser().GetUserDn(), _configRepository.GetAdminUser().GetUserAttribute("userPassword")[0]); _ldapConnection = LdapConnectionFactory.GetLdapConnection(_configRepository); _ldapConnection.Bind(adminCredential); _observers.ForEach(x => x.SetLdapConnection(_ldapConnection)); _logger.Write(_logger.BuildLogMessage(SuccessConnectionMessage(adminCredential), LdapState.LdapConnectionSuccess)); return(LdapState.LdapConnectionSuccess); }
public void AddServer(LdapDirectoryIdentifier identifier, int maxConnections, int protocolVersion = 3, bool ssl = false, double?timeout = null, NetworkCredential credentials = null, AuthType?authType = null) { var serverName = identifier.Servers[0]; var factory = new LdapConnectionFactory(serverName); if (credentials != null) { factory.AuthenticateAs(credentials); } if (authType.HasValue) { factory.AuthenticateBy(authType.Value); } if (timeout.HasValue) { factory.ConnectionTimeoutIn(timeout.Value); } factory.ProtocolVersion(protocolVersion); if (identifier.FullyQualifiedDnsHostName) { factory.ServerNameIsFullyQualified(); } if (identifier.Connectionless) { factory.UseUdp(); } if (ssl) { factory.UseSsl(); } factory.UsePort(identifier.PortNumber); _servers[serverName] = new ServerPoolMemberConnectionFactory(serverName, factory, maxConnections); }
public void Can_Reset_And_Change_Password() { var connectionFactory = new LdapConnectionFactory(ServerName); connectionFactory //.AuthenticateBy(AuthType.Basic) //.AuthenticateAs(new NetworkCredential("CN=AlphaUser,CN=Users,CN=Employees,DC=Northwind,DC=local", "test")) .AuthenticateBy(AuthType.Negotiate); using (var connection = connectionFactory.GetConnection()) { var user = connection.Query("CN=Employees,DC=Northwind,DC=local") .Select("distinguishedname") .FirstOrDefault(da => Filter.Equal(da, "cn", "TestUser2", false)); var newPassword = GetPasswordData("my pass"); user.Set("unicodePwd", newPassword); connection.Update(user, _configuration.Log); } using (var connection = connectionFactory.GetConnection()) { var user = connection.Query("CN=Employees,DC=Northwind,DC=local") .Select("distinguishedname") .FirstOrDefault(da => Filter.Equal(da, "cn", "TestUser2", true)); var currentPassword = GetPasswordData("my pass"); var newPassword = GetPasswordData("new pass"); user.AddModification(currentPassword.ToDirectoryModification("unicodePwd", DirectoryAttributeOperation.Delete)); user.AddModification(newPassword.ToDirectoryModification("unicodePwd", DirectoryAttributeOperation.Add)); connection.Update(user, _configuration.Log); } }
private byte[] GetActiveDirectoryBinaryImage(string assignedTo) { LdapConnectionFactory factory = new LdapConnectionFactory( ConfigurationManager.AppSettings["LdapServerName"] + ":" + ConfigurationManager.AppSettings["LdapServerPort"]); using (DirectoryContext context = new DirectoryContext(factory.GetConnection(), disposeOfConnection: true)) { var userMapping = new { DistinguishedName = string.Empty, Cn = string.Empty, givenName = string.Empty, ObjectGuid = default(Guid), Members = default(string[]), jpegPhoto = default(byte[]), thumbnailPhoto = default(byte[]) }; List<string> words = assignedTo.Split(' ').ToList(); List<string> wordsToRemove = new List<string>(); foreach (string word in words) { if (word.Length == 1) { wordsToRemove.Add(word); } else { if (word.Last() == '.') { wordsToRemove.Add(word); } } } words = words.Except(wordsToRemove).Distinct().ToList(); List<dynamic> users = new List<dynamic>(); foreach (string word in words) { var user = context.Query(userMapping, ConfigurationManager.AppSettings["LdapFilter"], objectClass: "User") .Where(r => r.Cn.Contains(word) && (r.Cn.StartsWith(word) || r.Cn.EndsWith(word)) && r.thumbnailPhoto != null) .FirstOrDefault(); if (user != null) { users.Add(user); if (users.Count == 2 && users[0].ObjectGuid == users[1].ObjectGuid) { users.RemoveAt(1); break; } } } if (users.Count == 1) { return users[0].thumbnailPhoto; } // Not unique object guids - try some comparisons of results (last resort) dynamic unknownUser = users.FirstOrDefault(x => words.All(y => x.DistinguishedName.Contains(y))); return unknownUser != null ? unknownUser.thumbnailPhoto : null; } }