identityserver4 - 如何将自定义身份验证提供程序集成到 IdentityServer4

标签 identityserver4

是否可以以某种方式扩展 IdentityServer4 以运行自定义身份验证逻辑?我需要针对几个现有的自定义身份系统验证凭据,并且很难找到这样做的扩展点(他们使用自定义协议(protocol))。
所有这些现有系统都具有客户端知道的 API key 的概念。 IdentityServer 的工作现在应该是验证这个 API key 并从系统中提取一些现有的声明。
我想做这样的事情:

POST /connect/token
    custom_provider_name=my_custom_provider_1&
    custom_provider_api_key=secret_api_key

然后我按逻辑调用my_custom_provider_1 ,验证 API key ,获取声明并将它们传递回 IdentityServer 流以完成其余工作。

这可能吗?

最佳答案

我假设您可以控制客户端及其发出的请求,因此您可以对您的身份服务器进行适当的调用。

可以使用自定义身份验证逻辑,毕竟 ResourceOwnerPassword流程就是这样:客户端将信息传递给 Connect/token 端点,您编写代码来确定该信息的含义并确定这是否足以验证该客户端。不过,您肯定会偏离常规去做您想做的事情,因为约定说客户端传递的信息是 username。和 password .

在您的 Startup.ConfigureServices您需要添加自己的 IResourceOwnerPasswordValidator 实现,有点像这样:

services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

然后在ValidateAsync该类的方法你可以做任何你喜欢的逻辑来决定是否设置context.Result走向成功GrantValidationResult ,或者失败的。在该方法中可以帮助您的一件事是 ResourceOwnerPasswordValidationContext有权访问原始请求。因此,您添加到对连接/ token 端点的原始调用中的任何自定义字段都将可供您使用。您可以在此处添加自定义字段(提供者名称、api key 等)。

祝你好运!

编辑:以上可以工作,但实际上是在滥用标准授权/流程。 OP 发现的使用 IExtensionGrantValidator 的方法要好得多。界面来滚动您自己的授权类型和身份验证逻辑。例如:

从客户端调用身份服务器:
POST /connect/token
grant_type=my_crap_grant&
scope=my_desired_scope&
rhubarb=true&
custard=true&
music=ska

向 DI 注册您的延期补助金:

services.AddTransient<IExtensionGrantValidator, MyCrapGrantValidator>();

并实现您的授权验证器:

public class MyCrapGrantValidator : IExtensionGrantValidator
{
    // your custom grant needs a name, used in the Post to /connect/token
    public string GrantType => "my_crap_grant";

    public async Task ValidateAsync(ExtensionGrantValidationContext context)
    {
        // Get the values for the data you expect to be used for your custom grant type
        var rhubarb = context.Request.Raw.Get("rhubarb");
        var custard = context.Request.Raw.Get("custard");
        var music = context.Request.Raw.Get("music");

        if (string.IsNullOrWhiteSpace(rhubarb)||string.IsNullOrWhiteSpace(custard)||string.IsNullOrWhiteSpace(music)
        {
            // this request doesn't have the data we'd expect for our grant type
            context.Result = new     GrantValidationResult(TokenRequestErrors.InvalidGrant);
            return Task.FromResult(false);
        }

        // Do your logic to work out, based on the data provided, whether 
        // this request is valid or not
        if (bool.Parse(rhubarb) && bool.Parse(custard) && music=="ska")
        {
            // This grant gives access to any client that simply makes a 
            // request with rhubarb and custard both true, and has music 
            // equal to ska. You should do better and involve databases and 
            // other technical things
            var sub = "ThisIsNotGoodSub";
            context.Result = new GrantValidationResult(sub,"my_crap_grant");
            Task.FromResult(0);
        }

        // Otherwise they're unauthorised
        context.Result = new GrantValidationResult(TokenRequestErrors.UnauthorizedClient);
        return Task.FromResult(false);
    }
}

关于identityserver4 - 如何将自定义身份验证提供程序集成到 IdentityServer4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43809881/

相关文章:

.net-core - OpenId Connect 与 IdentityServer 和 Identity 有什么关系?

javascript - JavasCript 单点注销/后台 channel 注销

asp.net - 对于使用 OpenIdConnect 身份验证的 WebForms 客户端,LogoutUri 应该是什么?

asp.net-core - IdentityServer4 + Asp.Net Core Identity - 将身份映射到应用程序数据库用户

c# - 用户似乎没有在 dotnet core 2.0 中的“Use”管道中进行身份验证

cors - IdentityServer 4 2.0 userInfo "No ' Access-Control-Allow-Origin' header 存在于请求的资源上。”

ssl - 如何在 IIS 中针对特定 URL 的所有请求要求双向 TLS

azure - 为什么我的 IdentityServer4 应用程序无法在 Azure WebApp 托管环境中启动?

asp.net - 使用 IdentityServer 4 和 ASP.NET Web 应用程序 (.NET Framework)

asp.net-core - 为登录设置 IdentityServer4 Cookies 的正确方法,用于 API 授权的 JWT token