我想使用.NET命名空間.NET System.DirectoryServices.Protocol
獲得LDAP認證工作。 (這是必要的,因爲我根本無法使用System.DirectoryServices
與我們的客戶端的LDAP服務器進行通信來獲得我現有的代碼。)DirectoryAttribute值應該是整數,而不是字符串
我已經設法使所有的工作,除了當我嘗試並返回userAccountControl
屬性用戶帳戶,它返回值的字符串表示形式,當它應該返回一個整數。 (在Active Directory的值肯定是一個整數。)
在下面的代碼,如果我斷點userAccountFlags = (int)attrib[0];
的attrib[0]
類型是一個字符串。爲什麼?
(我可以很容易地通過int.TryParse((string)attrib[0], out userAccountFlag);
取代它「修復」,但更希望知道爲什麼它的發生不是圍繞工作使用。)
var ident = new LdapDirectoryIdentifier(domain, (ssl ? 636 : 389));
using (var conn = new LdapConnection(ident))
{
conn.Credential = new NetworkCredential(domainUsername, domainPassword);
conn.AuthType = AuthType.Basic;
if (ssl)
{
conn.SessionOptions.SecureSocketLayer = true;
conn.SessionOptions.VerifyServerCertificate = (connection, certificate) => true;
}
conn.Bind();
var request = new proto.SearchRequest(rootDN, testDN, SearchScope.Subtree);
request.Attributes.Add("userAccountControl");
request.SizeLimit = 1;
var response = (SearchResponse)conn.SendRequest(request);
if (response.Entries.Count != 0)
{
int userAccountFlags = int.MinValue;
foreach (proto.SearchResultEntry entry in response.Entries)
{
foreach (proto.DirectoryAttribute attrib in entry.Attributes.Values)
{
if (attrib.Name == "userAccountControl" && attrib.Count > 0)
{
// The following line breaks, as "attrib[0] is string = true"
userAccountFlags = (int)attrib[0];
break;
}
}
}
}
}