1. 程式人生 > 其它 >C# LDAP工具類

C# LDAP工具類

/// <summary>
/// LDAP工具類
/// </summary>
public static class LDAPUtil
{
public static string Domain = "SHTAI";//域名稱
public static string Host = "ip";//域伺服器地址
public static string BaseDC = "dc=SHTAI,dc=CN";//根據上面的域伺服器地址,每個點拆分為一個DC,例如上面的apac.contoso.com,拆分後就是DC=apac,DC=contoso,DC=com
public static int Port = 389;//域伺服器埠,一般預設就是389
public static string DomainAdminUser = "222222";//域管理員賬號使用者名稱,如果只是驗證登入使用者,不對域做修改,可以就是登入使用者名稱
public static string DomainAdminPassword = "111111";//域管理員賬號密碼,如果只是驗證登入使用者,不對域做修改,可以就是登入使用者的密碼

public static Dictionary<string, string> GetUsers(string username, string password)
{
Dictionary<string, string> users = new Dictionary<string, string>();
try
{
using (var conn = new LdapConnection())
{
conn.Connect(Host, Port);
conn.Bind(Domain + "\\" + username, password);//這裡使用者名稱或密碼錯誤會丟擲異常LdapException
var root = conn.GetRootDseInfo();
var searchFilter = $"(sAMAccountName={username})";
searchFilter = null;
var entities =
conn.Search(BaseDC, LdapConnection.ScopeSub, searchFilter,//注意一個多的空格都不能打,否則查不出來
new string[] { "sAMAccountName", "mail" }, false);


LdapEntry entity = null;
while (entities.HasMore())
{
var sAMAccountName = "";
var mail = "";
try
{
entity = entities.Next();
var attributeSet = entity.GetAttributeSet();
if (!attributeSet.ContainsKey("sAMAccountName"))
{
continue;
}
sAMAccountName = entity.GetAttribute("sAMAccountName")?.StringValue;
if (!sAMAccountName.StartsWith("ST"))
{
continue;
}
if (!attributeSet.ContainsKey("mail"))
{
//continue;
}
else
{
mail = entity.GetAttribute("mail")?.StringValue;
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
continue;
}

Console.WriteLine($"User name : {sAMAccountName}");//james
Console.WriteLine($"User mail address : {mail}");//[email protected]
if (!users.ContainsKey(sAMAccountName))
{
users.Add(sAMAccountName, mail);
}

}

conn.Disconnect();
return users;
}
}
catch (LdapException ldapEx)
{
string message = ldapEx.Message;

return users;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return users;
}
}
public static bool Validate(string username, string password)
{
try
{
using (var conn = new LdapConnection())
{
conn.Connect(Host, Port);
conn.Bind(Domain + "\\" + username, password);//這裡使用者名稱或密碼錯誤會丟擲異常LdapException

var entities =
conn.Search(BaseDC, LdapConnection.ScopeSub,
$"sAMAccountName={username}",//注意一個多的空格都不能打,否則查不出來
new string[] { "sAMAccountName", "cn", "mail" }, false);

string userDn = null;
while (entities.HasMore())
{
var entity = entities.Next();
var sAMAccountName = entity.GetAttribute("sAMAccountName")?.StringValue;
var cn = entity.GetAttribute("cn")?.StringValue;
//var mail = entity.GetAttribute("mail")?.StringValue;

Console.WriteLine($"User name : {sAMAccountName}");//james
Console.WriteLine($"User full name : {cn}");//James, Clark [james]
//Console.WriteLine($"User mail address : {mail}");//[email protected]

//If you need to Case insensitive, please modify the below code.
if (sAMAccountName != null && sAMAccountName == username)
{
userDn = entity.Dn;
break;
}
}
if (string.IsNullOrWhiteSpace(userDn)) return false;
conn.Bind(userDn, password);//這裡使用者名稱或密碼錯誤會丟擲異常LdapException
// LdapAttribute passwordAttr = new LdapAttribute("userPassword", password);
// var compareResult = conn.Compare(userDn, passwordAttr);
conn.Disconnect();
return true;
}
}
catch (LdapException ldapEx)
{
string message = ldapEx.Message;

return false;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
}

}