ASP.NET 核心和本地 AD 身份验证
ASP.NET Core and on premise AD authentication
我想尝试在我的工作场所使用 ASP.NET Core MVC 或 Web API,但我们只有 Active Directory 才能进行身份验证和授权。是否有任何解决方案可以使用本地 AD 解决它,或者我们必须更改 Java?我知道这个问题不是原创的,但我想得到一个简单的答案,谢谢。
截至今天,System.DirectoryServices
在 ASP.NET Core 中尚不可用。您可以阅读更多 here.
在此期间,您可以使用Novell.Directory.Ldap.NETStandard
。例如,
public bool ValidateUser(string domainName, string username, string password)
{
string userDn = $"{username}@{domainName}";
try
{
using (var connection = new LdapConnection {SecureSocketLayer = false})
{
connection.Connect(domainName, LdapConnection.DEFAULT_PORT);
connection.Bind(userDn, password);
if (connection.Bound)
return true;
}
}
catch (LdapException ex)
{
// Log exception
}
return false;
}
由于动人的片段太多,我创作了a sample project at GitHub。
Microsoft 已发布 System.DirectoryServices
的 pre-release 版本。您可以使用以下命令从 NuGet 包管理器获取它:
Install-Package System.DirectoryServices -Version 4.5.0-preview1-25914-04
到目前为止,这对我来说工作正常。
我想尝试在我的工作场所使用 ASP.NET Core MVC 或 Web API,但我们只有 Active Directory 才能进行身份验证和授权。是否有任何解决方案可以使用本地 AD 解决它,或者我们必须更改 Java?我知道这个问题不是原创的,但我想得到一个简单的答案,谢谢。
截至今天,System.DirectoryServices
在 ASP.NET Core 中尚不可用。您可以阅读更多 here.
在此期间,您可以使用Novell.Directory.Ldap.NETStandard
。例如,
public bool ValidateUser(string domainName, string username, string password)
{
string userDn = $"{username}@{domainName}";
try
{
using (var connection = new LdapConnection {SecureSocketLayer = false})
{
connection.Connect(domainName, LdapConnection.DEFAULT_PORT);
connection.Bind(userDn, password);
if (connection.Bound)
return true;
}
}
catch (LdapException ex)
{
// Log exception
}
return false;
}
由于动人的片段太多,我创作了a sample project at GitHub。
Microsoft 已发布 System.DirectoryServices
的 pre-release 版本。您可以使用以下命令从 NuGet 包管理器获取它:
Install-Package System.DirectoryServices -Version 4.5.0-preview1-25914-04
到目前为止,这对我来说工作正常。