【发布时间】:2017-10-27 08:31:21
【问题描述】:
我正在使用 C# Core 2,使用 Active Directory 作为 Novell 的身份验证方法 - 我已经根据密码部分验证用户,如果 AD 中的用户名和密码正确,则验证他们。
我想获取登录用户的用户组,以增加安全性,就像[Authorize(roles="*")]would 一样。
以下代码是我目前所拥有的,我知道连接是正确的,但我无法在 SearchResults 中得到任何结果,它总是以 0 的计数返回。
我哪里出错了?以前没有任何 Active Directory 暴露。
搜索功能:
int searchScope = LdapConnection.SCOPE_BASE;
string searchFilter = "(CN = " + username + ")";
string searchBase = "OU=Users,OU=TOD,OU=Departments,DC=domain,DC=com";
// folder structure Users/TOD/Departments/List of people
// reading members of dynamic group could take long so set timeout to 10 seconds
LdapSearchConstraints constraints = new LdapSearchConstraints();
constraints.TimeLimit = 10000;
#region connection
string host = "mydomain.com";
string un = "mydomain\\" + username;
int port = 389;
int version = LdapConnection.Ldap_V3;
var conn = new LdapConnection();
conn.SecureSocketLayer = false;
conn.Connect(host, port);
conn.Bind(version, un, pass); //parsed in through function params
#endregion
LdapSearchResults searchResults = conn.Search(
searchBase,
searchScope,
searchFilter,
null, // no specified attributes
false, // return attr and value
constraints);
// always returns a search with 0 count
更新:
我间歇性地收到 LDAP 连接错误,并且在其他情况下,搜索返回为空 - 我不知道这是否与等待有关,但它会立即达到我在 conn.Disconnect() 设置的断点而不是比 while 循环中的任何断点都多。
[TestMethod]
public void SearchForUserDepartentTest()
{
var users = new Dictionary<string,string>();
var count = 0;
string searchFilter = "(objectclass=*)";
string searchBase = "OU=Departments,DC=domain,DC=com"; //ou=users, ou=TOD
// reading members of dynamic group could take long so set timeout to 10 seconds
LdapSearchConstraints constraints = new LdapSearchConstraints();
constraints.TimeLimit = 10000;
#region connection
string host = "domain.com";
string un = "domain\\doatemp2";
string pass = "****";
int port = 389;
int version = LdapConnection.Ldap_V3;
var conn = new LdapConnection
{
SecureSocketLayer = false
};
conn.Connect(host, port);
conn.Bind(version, un, pass);
#endregion
try
{
LdapSearchResults searchResults = conn.Search(
searchBase,
LdapConnection.SCOPE_ONE,
searchFilter,
null, // no specified attributes
false, // return attr and value
constraints);
while (searchResults.hasMore())
{
// doesn't hit in here intermittently
count++;
var nextEntry = searchResults.next();
nextEntry.getAttributeSet();
var attr = nextEntry.getAttribute("NAME");
if (attr == null)
{
users.Add("Distinguished Name", nextEntry.getAttribute("distinguishedName").StringValue);
}
else
{
users.Add((nextEntry.getAttribute("SAMACCOUNTNAME") == null)? "NULL ACC Name " + count : nextEntry.getAttribute("SAMACCOUNTNAME").StringValue
,(nextEntry.getAttribute("DISTINGUISHEDNAME") == null)? "NULL DN" + count : nextEntry.getAttribute("distinguishedName").StringValue);
}
}
}
catch (LdapException ldapEx)
{
ldapEx.ToString(); // ocassional time outs
}
catch (Exception ex)
{
ex.ToString();
}
conn.Disconnect(); // when run hits the break point here, missing out the anything in searchResults.hasMore()
}
更新 2:
最新代码。
[TestMethod]
public void SearchForUserDepartentTest()
{
var users = new Dictionary<string,string>();
var count = 0;
string searchFilter = "(objectclass=*)";
string searchBase = "OU=Departments,DC=domain,DC=com"; //ou=users, ou=TOD
// reading members of dynamic group could take long so set timeout to 10 seconds
LdapSearchConstraints constraints = new LdapSearchConstraints();
constraints.TimeLimit = 30000;
#region connection information
string host = "domain";
string un = "domain\\doatemp2";
string pass = "";
int port = 389;
int version = LdapConnection.Ldap_V3;
#endregion
try
{
using (var conn = new LdapConnection { SecureSocketLayer = false })
{
conn.Connect(host, port);
conn.Bind(version, un, pass);
LdapSearchResults searchResults = conn.Search(
searchBase,
LdapConnection.SCOPE_SUB,
searchFilter,
null, // no specified attributes
false, // return attr and value
constraints);
while (searchResults.hasMore())
{
count++;
var nextEntry = searchResults.next();
nextEntry.getAttributeSet();
var attr = nextEntry.getAttribute("NAME");
if (attr == null)
{
users.Add("Distinguished Name", nextEntry.getAttribute("distinguishedName").StringValue);
}
else
{
users.Add((nextEntry.getAttribute("SAMACCOUNTNAME") == null) ? "NULL ACC Name " + count : nextEntry.getAttribute("SAMACCOUNTNAME").StringValue,
(nextEntry.getAttribute("DISTINGUISHEDNAME") == null) ? "NULL DN" + count : nextEntry.getAttribute("distinguishedName").StringValue);
}
}
}
}
catch (LdapException ldapEx)
{
ldapEx.ToString(); // ocassional time outs
}
catch (Exception ex)
{
ex.ToString();
}
var check = users;
}
更新 3: 在测试环境造成不利影响的情况下使用 Core 控制台应用程序。使用下面的代码读取 LdapConnection timeout 85
公共静态无效 SearchForUserDepartent() { var users = new Dictionary(); 变量计数 = 0;
string searchFilter = "(objectclass=*)";//string.Empty;
string searchBase = "OU=Users,OU=TOD,OU=Departments,DC=domain,DC=com";
LdapSearchConstraints constraints = new LdapSearchConstraints
{
TimeLimit = 15000
};
#region connection information
string host = "dm1.domain.com";
string un = "domain\\doatemp2";
string pass = "password";
int port = 389;
#endregion
try
{
using (var conn = new LdapConnection { SecureSocketLayer = false })
{
conn.Connect(host, port);
conn.Bind(un, pass);
LdapSearchResults searchResults = conn.Search(
searchBase,
LdapConnection.SCOPE_SUB,
searchFilter,
null, // no specified attributes
false, // return attr and value
constraints);
while (searchResults.hasMore())
{
count++;
var nextEntry = searchResults.next(); // hits and then goes to timeout
nextEntry.getAttributeSet();
Console.WriteLine("Distinguished Name:" + nextEntry.getAttribute("distinguishedName").StringValue);
Console.ReadKey();
}
}
}
catch (LdapException ldapEx)
{
Console.WriteLine(ldapEx.ToString()); // ocassional time outs
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
foreach(var u in users)
{
Console.WriteLine("Key:" + u.Key.ToString() + " | Value:" + u.Value.ToString());
}
Console.ReadKey();
}
【问题讨论】:
-
@Jaybird 这部分是我一直在使用的,但我在 searchResults 中什么也没得到,我认为我使用 searchBase、searchFilter 和/或 searchScope 不正确。
-
@PurpleSmurph,我忘了说。尝试使用 CN=Users,DC=domain,DC=com 或 CN=Users,OU=TOD,OU=Departments,DC=domain,DC=com
标签: c# asp.net-core active-directory ldap