public UserInformations Authenticate(string username, string password, ILogger logger) { UserInformations userInformations = null; logger.LogDebug("LDAP: Trying connection to LDAP"); var connection = new LdapConnection { SecureSocketLayer = _options.SecureSocketLayer }; string[] attributes = new[] { "samaccountname", "displayname", "uidnumber", "mail" }; connection.Connect(_options.HostName, _options.Port); connection.Bind(username, password); if (connection.Bound) { logger.LogDebug("LDAP: Connected to LDAP with username " + username); var results = connection.Search(_options.BaseDN, LdapConnection.ScopeSub, $"samaccountname={username.Split("@")[0]}", attributes, false); if (results.HasMore()) { var attributeSet = results.Next().GetAttributeSet(); logger.LogDebug("LDAP: LDAP has data for user " + username); userInformations = new UserInformations { Name = attributeSet.GetAttribute("displayname")?.StringValue, Login = attributeSet.GetAttribute("samaccountname")?.StringValue, PersonalNumber = attributeSet.GetAttribute("uidnumber")?.StringValue, Email = attributeSet.GetAttribute("mail")?.StringValue, }; } } return(userInformations); }
public async Task <bool> AddLdapUser(UserInformations informations) { string[] names = informations.Name.Split(" "); User user = new User(informations.Email, names[0], names[1]) { IsLdapUser = true, EmailConfirmed = true }; var addResult = await AddUserAsync(user, GetDefaultLdapPassword()); return(addResult.Succeeded); }
public UserInformations GetUserFromLDAP(string login, string password, ILogger logger) { login += "@fri.uniza.sk"; OptionsLdap options = new OptionsLdap { SecureSocketLayer = bool.Parse(_ldapSettings.SecureSocketLayer), BaseDN = _ldapSettings.BaseDN, HostName = _ldapSettings.HostName, Port = int.Parse(_ldapSettings.Port), }; AuthenticatorLdap authenticatorLdap = new AuthenticatorLdap(options); UserInformations informations = authenticatorLdap.Authenticate(login, password, logger); return(informations); }