IdentityServer4并调用自定义函数

时间:2017-02-07 15:49:42

标签: c# identityserver4

我想要实现的目标是:设置一个IdentityServer4实例,将用户名/密码的身份验证交给位于另一个.net程序集中的函数。我知道OpenID Connect是一个选项,但是提出一个自定义IdP给人的印象就是在一个小山丘上建造一座山。

有没有使用Identityserver实现此目的的简单方法?

1 个答案:

答案 0 :(得分:1)

是。在ConfigureServices中的Startup方法中,您需要注册自己的IProfileServiceIResourceOwnerPasswordValidator实施。例如:

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定义中所定义),然后自行调用任何程序集/回购你需要找出这些索赔的细节。