IdentityServer4和获取多个应用程序以共享身份验证

时间:2019-11-12 03:03:58

标签: asp.net-core single-sign-on identityserver4

在使用身份服务器实现单点登录或类似功能的不同示例中,我遇到了很多麻烦。给定

的IDServ Startup.cs配置
var builder = services.AddIdentityServer()
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApis())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());

和两个客户:

services.AddMvc();

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")
                .AddOpenIdConnect("oidc", options =>
                {
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;

                    options.ClientId = "mvc";
                    options.SaveTokens = true;
                });

+

services.AddMvcCore()
                .AddAuthorization()
                .AddJsonFormatters();

            services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", options =>
                {
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;

                    options.Audience = "api1";
                });

当请求没有有效令牌时,我总是遇到使第二个客户端重定向到Auth服务器的问题。我以上面的示例为例,但是似乎client2是否像第一个一样使用AddCookie,还是它们中的一个或两个都使用AddJwtBearer,等等。混搭配置的不同组合我觉得有必要问一下配置的重要部分是什么,这些重要部分导致客户端重定向到IdentityServer的身份验证/登录页面,以及如何使多个客户端之间相互共享Auth发挥作用。

我已经找到了IdentityServer规范示例(https://github.com/IdentityServer/IdentityServer4/tree/aspnetcore2/samples/Quickstarts/),但是它们没有对此进行很好的介绍:如上述示例(我从QS#3中摘录了片段,只有第一个客户端重定向了) ,而第二个将只有401(如果尚未具有有效的承载令牌)。

1 个答案:

答案 0 :(得分:0)

根据您的代码,第一件事是在IdentityServer4中仅使用AddOpenIdConnect配置了一个客户端。

您提到的第二个客户端是IDS的API资源/受众。通过AddOpenIdConnect使用提到的客户端令牌对它们进行身份验证。

更好地查看以下示例。 http://docs.identityserver.io/en/latest/quickstarts/3_aspnetcore_and_apis.html