Asp.net core 如何使用identity server对active directory用户进行身份验证?
我想使用identity server对我的用户进行身份验证和授权。 我只希望用于用户资源使用active directory用户以及我希望从asp.net identity使用的角色等 另外,我不想使用windows身份验证进行身份验证 我正在使用identity server 4和asp.net core 3.2Asp.net core 如何使用identity server对active directory用户进行身份验证?,asp.net-core,active-directory,identityserver4,Asp.net Core,Active Directory,Identityserver4,我想使用identity server对我的用户进行身份验证和授权。 我只希望用于用户资源使用active directory用户以及我希望从asp.net identity使用的角色等 另外,我不想使用windows身份验证进行身份验证 我正在使用identity server 4和asp.net core 3.2 services.AddIdentityServer().AddDeveloperSigningCredential() //.AddTestUsers(Config.Ge
services.AddIdentityServer().AddDeveloperSigningCredential()
//.AddTestUsers(Config.GetUsers())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryClients(Config.GetClients());
首先,您需要安装以下软件包才能使用ActiveDirectory功能
Install-Package Microsoft.Windows.Compatibility
其次,您需要实现IResourceOwnerPasswordValidator,并使用其中的ActiveDirectory检查用户密码
public class ActiveDirectoryResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
const string LDAP_DOMAIN = "exldap.example.com:5555";
using (var pcontext = new PrincipalContext(ContextType.Domain, LDAP_DOMAIN, "service_acct_user", "service_acct_pswd"))
{
if (pcontext.ValidateCredentials(context.UserName, context.Password))
{
// user authenticated and set context.Result
}
}
// User not authenticated and set context.Result
return Task.CompletedTask;
}
}
然后在Startup.cs上注册它
services.AddSingleton<IResourceOwnerPasswordValidator, ActiveDirectoryResourceOwnerPasswordValidator>();
services.AddSingleton();