多个 IdentityServer4 服务的最佳实践

Best practice on multiple IdentityServer4 services

我有两个身份服务器和一个 Web API。 我想做的是让 API 通过一个或两个 IdentityServers 进行身份验证,并且能够在其中一个出现故障时进行切换。如果可能的话,我还希望能够在运行时添加一个新的 IdentityServer。

这里有什么最佳实践吗?

目前看起来像这样。

        app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = $"http://localhost:5000",
            ScopeName = "my.scope",
            RequireHttpsMetadata = false,
            ScopeSecret = "secret",
        });

如果我在端口 5000 关闭 IdentityServer,我将无法再使用 API。这是意料之中的事情。

我不确定这是否是解决问题的好方法。但这是一种方式。 我要求 "first identityservice" 的路由服务在选项中设置 Authroity。 然后我添加一个自定义的 IntrospectionBackChannelHandler

        app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = $"http://{v.Address}:{v.Port}",
            IntrospectionBackChannelHandler = new CustomIntrospectionBackChannelHandler(consulService)

因为我所有的身份服务器看起来都一样,但地址不同,所以我真的不必费心再做一次授权。

在自定义内省....我检查每个内省并将其发送到 "correct" 身份服务器。如果它不起作用,我会尝试另一个身份服务器。

var qs = await request.Content.ReadAsStringAsync(); var queryDic = QueryHelpers.ParseQuery(等待 request.Content.ReadAsStringAsync());

        var token = queryDic["token"];
        var client_id = queryDic["client_id"];
        var client_secret = queryDic["client_secret"];
        var iRequest = new IntrospectionRequest
        {
            ClientId = client_id,
            ClientSecret = client_secret,
            TokenTypeHint = "access_token",
            Token = token
        };

        IntrospectionResponse result = null;

        var svc = await _Consul.GetService(OrbitServices.IdentityServer);
        result = await TrySendAsync(iRequest, svc);
        if (!result.IsActive && result.IsError)
        {
            svc = await _Consul.GetService(OrbitServices.IdentityServer, true);
            result = await TrySendAsync(iRequest, svc);
        }

        var message = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent(result.Raw, Encoding.UTF8, "application/json")
        };

        return message;