#c# #.net-core #ldap #novell
Вопрос:
Я видел несколько вопросов, которые предполагают что-то связанное с пользователями и группами, но я понятия не имею, каковы их варианты использования.
Все, что я пытаюсь сделать, — это найти группу, с которой связан пользователь после успешной аутентификации.
используя следующие:
public bool LogInViaLDAP(LoginDTO userForLoginDto)
{
var user = userForLoginDto.Username;
string userDn = $"cn={user},ou=users,ou=system";
using (var connection = new LdapConnection { SecureSocketLayer = _isSecureSocketLayer })
{
connection.ConnectionTimeout = 36000;
connection.Connect(_domain, _port);
connection.Bind(userDn, userForLoginDto.Password);
string[] requiredAttributes = { "cn", "sn", "ou" };
string searchFilter = "objectClass=inetOrgPerson";
//this is where I was attempting to find the user's group association.
var groups = SearchForGroup(connection, userDn, searchFilter, requiredAttributes, false);
if (connection.Bound)
return true;
}
return false;
}
HashSet<string> SearchForGroup(LdapConnection connection, string user, string searchFilter, string[] requiredAttributes, bool typesOnly)
{
var result = connection.Search(user, LdapConnection.ScopeSub, searchFilter, requiredAttributes, typesOnly);
LdapEntry nextEntry = null;
while (result.HasMore())
{
nextEntry = result.Next();
}
//This only seems th return the
//sn - surname and cn - common name.
var data = nextEntry.GetAttributeSet();
return new HashSet<string>();
}
Ответ №1:
Я полагал, что пакет Novell основан на фактическом языке запросов, который использует LDAP.
Поэтому я выбрал узел ou=группы в Apache Directory Studio и попытался найти своего пользователя оттуда, используя:
uniqueMember=cn=имя пользователя,подразделение=пользователи,подразделение=система
Это вернуло группу, с которой связан пользователь, поэтому я перешел к ней.
string[] requiredAttributes = { "cn" };
var groups = SearchForGroup(connection, "ou=groups,ou=system", "uniqueMember=cn=username,ou=users,ou=system", requiredAttributes, false);
На приведенном выше фрагменте показано, как параметры необходимо было передать в моем коде c#, чтобы повторить то, что я сделал в Directory Studio
HashSet<string> SearchForGroup(LdapConnection connection, string entryPoint, string searchFilter, string[] requiredAttributes, bool typesOnly)
{
var result = connection.Search(entryPoint, LdapConnection.ScopeSub, searchFilter, requiredAttributes, typesOnly);
LdapEntry nextEntry = null;
var groups = new HashSet<string>();
foreach (var group in result)
{
var attribute = group.GetAttribute("cn");
groups.Add(attribute.StringValue);
}
return groups;
}