【问题标题】:How to use identity server for authenticating active directory users?如何使用身份服务器对活动目录用户进行身份验证?
【发布时间】:2020-03-20 15:57:07
【问题描述】:

我想使用 身份服务器 对我的用户进行身份验证和授权。 我只希望用户资源使用 活动目录用户 以及我想从 asp.net 身份使用的角色等。

我也不想使用windows身份验证来进行身份验证。

我正在使用 identity server 4asp.net core 3.2

services.AddIdentityServer().AddDeveloperSigningCredential()
    //.AddTestUsers(Config.GetUsers())
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryClients(Config.GetClients());

【问题讨论】:

    标签: asp.net-core active-directory identityserver4


    【解决方案1】:

    首先,您需要安装以下软件包才能使用 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>();
    

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多