oauth 2.0-[C#,.NET]:通过IdentityServer3通过LDAP验证用户



我需要一种方法来通过IdentityServer3验证组织内的用户(使用LDAP和Active Directory(,并授予他们访问资源的权限。

IdentityServer3似乎是OpenIDConnect协议的一个实现框架,它适用于身份验证和授权。

到目前为止,我已经能够验证硬编码用户,并使用InMemory实现获得JWT(JSON Web令牌(访问令牌。

请参考此示例:

https://rajdeep.io/2015/05/07/creating-a-single-sign-on-using-thinktecture-identity-server-part-1/

然而,在我的场景中,这并不是很有用,因为我必须验证大型组织中的用户(存储在活动目录中(,并向他们颁发访问资源的令牌。

以下是我所做的每个链接

(http://stackoverflow.com/questions/31536420/thinktecture-identityserver-v3-with-windowsauth(:

  1. 创建了一个实现IUserService的ActiveDirectoryUserService。

    namespace LDAPSSO
    {
    public class ActiveDirectoryUserService : IUserService
    {
     private const string DOMAIN = "company domain";
     public Task<AuthenticateResult> AuthenticateExternalAsync(ExternalIdentity externalUser, SignInMessage message)
    {
        return Task.FromResult<AuthenticateResult>(null);
    }
    public Task<AuthenticateResult> AuthenticateLocalAsync(string username, string password, SignInMessage message)
    {
        try
        {
            using (var pc = new PrincipalContext(ContextType.Domain, DOMAIN))
            {
                if (pc.ValidateCredentials(username, password))
                {
                    using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
                    { 
                        if (user != null)
                        {
                            return Task.FromResult(new AuthenticateResult(subject: Guid.NewGuid().ToString(), name: username));
                        }
                    }
                }
                // The user name or password is incorrect
                return Task.FromResult<AuthenticateResult>(null);
            }
        }
        catch
        {
            // Server error
            return Task.FromResult<AuthenticateResult>(null);
        }
      }
     }
    }
    

参考:"https://gist.github.com/tjrobinson/0ad6c790e90d7a385eb1">

  1. 创建了实现IClientStore的MyClientStore
  2. 创建了一个实现IScopeStore的MyScopeStore
  3. 向工厂注册如下:

    public class Factory
    {
     public static IdentityServerServiceFactory Configure()
     {
      var factory = new IdentityServerServiceFactory
         {
            UserService = new Registration<IUserService, ActiveDirectoryUserService>(), // Don't need, but mandatory for idsvr3
            ClientStore = new Registration<IClientStore, MyClientStore>(),
            ScopeStore = new Registration<IScopeStore, MyScopeStore>()
        };
      return factory;
    }
    

即使我能做到这一点,难道不需要将其与身份服务器提供的登录表单中用户输入的凭据进行比较吗?(请参见下图(:

授权服务器登录

我需要做些什么才能从IdentityServer3登录页面提取用户凭据并根据Active Directory进行验证?

这是正确的方法吗?请提出建议。

欢迎提供意见和建议。

提前谢谢!

我会将Active Directory实现为一个外部身份提供程序。

这样,您就不必维护任何自定义代码,并在锁定AD的情况下继续使用它们的LDAP功能。您可以通过在identity Server中禁用本地登录,或使用acr_values中的idp值,甚至基于每个客户端,将其作为唯一的身份提供程序。

最新更新