使用 System.DirectoryServices.AccountManagement 命名空间中的 PrincipalContext 类,可轻松地在 ASP.NET Core 6 应用程序中检查 Active Directory。 以下是示例代码:
using System.DirectoryServices.AccountManagement;
public bool ValidateUser(string domain, string username, string password)
{
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
return context.ValidateCredentials(username, password);
}
}
在上述示例代码中,必须传递以下参数:
此外,可以使用 PrincipalSearcher 类和 UserPrincipal 类在 Active Directory 中查找用户并检查用户是否属于某个组。具体可以参考 Microsoft 文档。