且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用LDAP通过ASP.NET Core针对Active Directory登录身份验证时出错

更新时间:2022-06-04 07:38:57

与您所引用的博客文章(已有2年历史), System.DirectoryServices System.DirectoryServices.AccountManagement 命名空间 ,因此可在.NET Core 2.x/3.x中使用.

Contrary to what is stated in that blog post you referenced (which is 2 years old), the System.DirectoryServices and System.DirectoryServices.AccountManagement namespace are in fact supported on .NETStandard 2.0 and thus usable in .NET Core 2.x/3.x.

查看相关的Nuget页面:

Check out the relevant Nuget page:

https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.7.0

因此,您可以非常轻松地使用常规"代码来验证用户凭据:

And thus, you can very easily use the "usual" code to validate user credentials:

using System.DirectoryServices.AccountManagement;

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "par"))
{
    // validate the user's credentials
    if (ctx.ValidateCredentials(userName, password)
    {
        // credentials are OK --> allow user in
    }
    else
    {
        // credentials aren't OK --> send back error message
    }
}