在 Ubuntu 18.04.4LTS 上的 System.DirectoryServices.Protocols 处调用 LdapConnection.SendRequest(SearchRequest req) 时出现分段错误(核心已转储)
Segmentation fault (core dumped) when calling LdapConnection.SendRequest(SearchRequest req) at System.DirectoryServices.Protocols on Ubuntu 18.04.4LTS
当我尝试从 Active Directory 获取用户信息时,LdapConnection.SendRequest(SearchRequest req) 导致:
Segmentation fault (core dumped)
在 Ubuntu 18.04.4 LTS(GNU/Linux 4.15.0-147-generic x86_64).
如果我删除行
searchRequest.TimeLimit = new TimeSpan(0, 1, 0);
LdapPal.SearchDirectory 方法 returns LdapError.TimeOut (-7)。并抛出:
System.DirectoryServices.Protocols.LdapException: The LDAP server returned an unknown error.
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at ConsoleApp16.Program.GetUserDetailsFromLdap(String username) in D:\source\repos\ConsoleApp16\ConsoleApp16\Program.cs:line 47
下面的代码在 Windows.
上没有任何问题
通过 LdapConnection.Bind(new NetworkCredential(username, password, domainName)) 验证用户名和密码没有任何问题。
代码如下:
private static string _baseDn = "OU=org1,DC=org1,DC=com,DC=tr";
private static LdapConnection _con = new(new LdapDirectoryIdentifier("org1.com.tr"), new NetworkCredential("#Username", "#Password", "#DomainName"));
static void Main(string[] args)
{
_con.AuthType = AuthType.Basic;
Console.ReadLine();
Console.WriteLine("Hello World!");
Console.WriteLine(TryLoginUser(@"#Username", "#Password","#DomainName"));
Console.WriteLine(GetUserDetailsFromLdap(@"#Username").Email);
}
private static bool TryLoginUser(string username, string password, string domainName)
{
try
{
_con.Bind(new NetworkCredential(username, password, domainName));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
return true;
}
private static LdapUser GetUserDetailsFromLdap(string username)
{
var searchFilter = $"(&sAMAccountName={username.Split('@')[0]})";
var searchRequest = new SearchRequest(_baseDn, searchFilter, SearchScope.Subtree, new string[] { "mail", "givenName", "sn", "sAMAccountName" });
searchRequest.TimeLimit = new TimeSpan(0, 1, 0);
try
{
var response = (SearchResponse)_con.SendRequest(searchRequest);
if (response?.ResultCode == ResultCode.Success)
{
var result = response?.Entries[0];
return new LdapUser()
{
Email = result.Attributes["mail"][0].ToString(),
FirstName = result.Attributes["givenName"][0].ToString(),
LastName = result.Attributes["sn"][0].ToString(),
UserName = result.Attributes["sAMAccountName"][0].ToString()
};
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
}
public class LdapUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
}
配置
System.DirectoryServices.Protocols 版本 5.0.0
运行时间:Microsoft.NETCore.App5.0.7
目标运行时:可移植
部署模式:便携式
更新
当使用 6.0.0-preview.7.21377.19 版本的 System.DirectoryServices.Protocols 而没有 SearchRequest.TimeLimit 属性 会导致:
System.DirectoryServices.Protocols.LdapException: The search filter is invalid.
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at ConsoleApp16.Program.GetUserDetailsFromLdap(String username) in D:\source\repos\ConsoleApp16\ConsoleApp16\Program.cs:line 47
private static LdapConnection _con = new(new LdapDirectoryIdentifier("org1.com.tr"));
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
var searchFilter =isWindows?$"(&sAMAccountName={username.Split('@')[0]})": $"(&(objectCategory=person)(objectclass=user)(sAMAccountName={username.Split('@')[0]}))";
如果我将 searchFilter 设置为操作系统条件,大多数时候方法都会成功执行,即使我没有为 LDAPConnection 对象设置凭据也是如此。
但是它继续随机抛出
System.DirectoryServices.Protocols.LdapException: The LDAP server returned an unknown error.
当我将 LdapConnection.SessionOptions.ProtocolVersion 设置为 3 时,Linux 上的一切也都按预期工作。
当我尝试从 Active Directory 获取用户信息时,LdapConnection.SendRequest(SearchRequest req) 导致:
Segmentation fault (core dumped)
在 Ubuntu 18.04.4 LTS(GNU/Linux 4.15.0-147-generic x86_64).
如果我删除行
searchRequest.TimeLimit = new TimeSpan(0, 1, 0);
LdapPal.SearchDirectory 方法 returns LdapError.TimeOut (-7)。并抛出:
System.DirectoryServices.Protocols.LdapException: The LDAP server returned an unknown error.
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at ConsoleApp16.Program.GetUserDetailsFromLdap(String username) in D:\source\repos\ConsoleApp16\ConsoleApp16\Program.cs:line 47
下面的代码在 Windows.
上没有任何问题通过 LdapConnection.Bind(new NetworkCredential(username, password, domainName)) 验证用户名和密码没有任何问题。
代码如下:
private static string _baseDn = "OU=org1,DC=org1,DC=com,DC=tr";
private static LdapConnection _con = new(new LdapDirectoryIdentifier("org1.com.tr"), new NetworkCredential("#Username", "#Password", "#DomainName"));
static void Main(string[] args)
{
_con.AuthType = AuthType.Basic;
Console.ReadLine();
Console.WriteLine("Hello World!");
Console.WriteLine(TryLoginUser(@"#Username", "#Password","#DomainName"));
Console.WriteLine(GetUserDetailsFromLdap(@"#Username").Email);
}
private static bool TryLoginUser(string username, string password, string domainName)
{
try
{
_con.Bind(new NetworkCredential(username, password, domainName));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
return true;
}
private static LdapUser GetUserDetailsFromLdap(string username)
{
var searchFilter = $"(&sAMAccountName={username.Split('@')[0]})";
var searchRequest = new SearchRequest(_baseDn, searchFilter, SearchScope.Subtree, new string[] { "mail", "givenName", "sn", "sAMAccountName" });
searchRequest.TimeLimit = new TimeSpan(0, 1, 0);
try
{
var response = (SearchResponse)_con.SendRequest(searchRequest);
if (response?.ResultCode == ResultCode.Success)
{
var result = response?.Entries[0];
return new LdapUser()
{
Email = result.Attributes["mail"][0].ToString(),
FirstName = result.Attributes["givenName"][0].ToString(),
LastName = result.Attributes["sn"][0].ToString(),
UserName = result.Attributes["sAMAccountName"][0].ToString()
};
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
}
public class LdapUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
}
配置
System.DirectoryServices.Protocols 版本 5.0.0
运行时间:Microsoft.NETCore.App5.0.7
目标运行时:可移植
部署模式:便携式
更新 当使用 6.0.0-preview.7.21377.19 版本的 System.DirectoryServices.Protocols 而没有 SearchRequest.TimeLimit 属性 会导致:
System.DirectoryServices.Protocols.LdapException: The search filter is invalid.
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at ConsoleApp16.Program.GetUserDetailsFromLdap(String username) in D:\source\repos\ConsoleApp16\ConsoleApp16\Program.cs:line 47
private static LdapConnection _con = new(new LdapDirectoryIdentifier("org1.com.tr"));
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
var searchFilter =isWindows?$"(&sAMAccountName={username.Split('@')[0]})": $"(&(objectCategory=person)(objectclass=user)(sAMAccountName={username.Split('@')[0]}))";
如果我将 searchFilter 设置为操作系统条件,大多数时候方法都会成功执行,即使我没有为 LDAPConnection 对象设置凭据也是如此。
但是它继续随机抛出
System.DirectoryServices.Protocols.LdapException: The LDAP server returned an unknown error.
当我将 LdapConnection.SessionOptions.ProtocolVersion 设置为 3 时,Linux 上的一切也都按预期工作。