我想要实现的目标是:设置一个IdentityServer4实例,将用户名/密码的身份验证交给位于另一个.net程序集中的函数。我知道OpenID Connect是一个选项,但是提出一个自定义IdP给人的印象就是在一个小山丘上建造一座山。
有没有使用Identityserver实现此目的的简单方法?
答案 0 :(得分:1)
是。在ConfigureServices
中的Startup
方法中,您需要注册自己的IProfileService
和IResourceOwnerPasswordValidator
实施。例如:
services.AddTransient<IProfileService, CustomProfileService>();
services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
ValidatedAsync
上的IResourceOwnerPasswordValidator
方法可以访问用户名/密码并调用您自己的程序集进行验证。然后从该方法返回GrantValidationResult
对象以指示成功/失败。
例如:
public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
logWriter.Debug("Validating resource owner password: " + context.UserName);
if (_userRepository.ValidatePassword(context.UserName, context.Password))
{
context.Result = new GrantValidationResult(context.UserName, "password");
return Task.FromResult(0);
}
logWriter.Debug("Unauthorised resource owner password for: " + context.UserName);
return Task.FromResult(new GrantValidationResult(TokenRequestErrors.UnauthorizedClient));
}
IProfileService
是您可以在上下文中设置声明的地方,例如通过检查已请求的内容(如ApiResource
定义中所定义),然后自行调用任何程序集/回购你需要找出这些索赔的细节。