我目前正在一个项目中,我试图建立一个基于IdentityServer4(https://github.com/IdentityServer/IdentityServer4)的服务,该服务通过LDAP查询本地Active Directory来对用户进行身份验证。
为实现这一点,我还在项目中添加了IdentityServer4.LdapExtension(https://github.com/Nordes/IdentityServer4.LdapExtension)。来自存储库的工作示例工作正常(https://github.com/Nordes/IdentityServer4.LdapExtension/tree/master/Sample/IdentityServer)-但是自定义逻辑是UI的一部分,我需要我的服务在没有任何UI的情况下进行操作。
只需添加
.AddLdapUsers<ActiveDirectoryAppUser>(Conf.GetSection("ldap"), UserStore.InMemory)
文档中所述的不会更改请求管道,因为从未执行所提供的登录/验证方法-它们仅由来自UI(AccountController)的调用触发。但是,正如我所说,我不想在此服务中集成任何UI,而是使用Token-Endpoint已提供的接口(带有client_id和client_secret的POST请求,带有JWT的响应)。
是否有一种方法可以集成LDAP身份验证,而无需重写可按需即用的重要部分?
答案 0 :(得分:1)
从您的问题看来,您似乎已经有一个username
和password
。请注意client_id
!= username
和client_secret
!= password
。 client_id
是客户端应用程序的身份。
您尝试使用的授权类型在使用授权端点时称为Resource Owner Password,在使用令牌端点时称为password。 此授予类型用于支持旧系统,不建议用于新开发。
您要执行以验证用户身份的代码位于LdapUserResourceOwnerPasswordValidator.cs中,如果您将正确的参数传递给令牌端点,则应执行该代码:
POST /连接/令牌
client_id=yourclientid& client_secret=yourclientsecret& grant_type=password& username=yourusername&password=yourusernamespassword
请参阅令牌端点文档:https://identityserver4.readthedocs.io/en/release/endpoints/token.html
您可以使用Identity Model帮助您进行令牌请求:
var response = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
{
Address = "https://demo.identityserver.io/connect/token",
ClientId = "yourclientid",
ClientSecret = "yourclientsecret",
UserName = "yourusername",
Password = "yourusernamespassword"
});
此内容记录在https://identitymodel.readthedocs.io/en/latest/client/token.html