我实现了一个单点登录 (SSO),其中两个应用程序作为客户端。 我希望用户通过 SSO 应用程序在第一个应用程序中进行身份验证,在第二个应用程序中不需要再次登录和身份验证。 (共享身份验证 cookie)。
信息:
localhost:44372 => SSO
localhost:44387 => SSO Client_1
localhost:44382 => SSO Client_2
All Projects are ASP.NET 5
SSO项目代码:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIndentityResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(TestUsers.Users);
}
配置.cs:
public class Config
{
public static IEnumerable<IdentityResource> GetIndentityResources()
{
//...
}
public static IEnumerable<Client> GetClients()
{
return new List<Client>()
{
new Client()
{
ClientId = "Client_1",
ClientName = "SSO Application",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = {"https://localhost:44387/signin-oidc"},
PostLogoutRedirectUris = {"https://localhost:44387/signout-callback-oidc"},
AllowedScopes = new List<string>()
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
},
new Client()
{
ClientId = "Client_2",
ClientName = "SSO Application 2",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = {"https://localhost:44382/signin-oidc"},
PostLogoutRedirectUris = {"https://localhost:44382/signout-callback-oidc"},
AllowedScopes = new List<string>()
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
}
};
}
}
如何在客户端之间共享身份验证?
Cookie 共享与 Identityserver、OpenId Connect 或任何其他 SSO 技术无关。它是关于服务器托管的应用程序,例如 ASP.NET(Core)MVC 或任何其他类似的应用程序。
身份验证 cookie 绑定到主机和可选路径,因此当应用程序紧密托管在一起时不会出现问题,但是 ASP.Net 将其身份验证令牌保留在 cookie 中,并且令牌通常绑定到 OIdC 会话,因此答案应该是:
当您想在 ASP.NET 中共享 cookie 时,您很可能会这样做 OIdC 中不需要两个单独的客户端
一个选项是使用两个应用程序的重定向 URL 的单一身份客户端,另一个选项是将两个应用程序合并为一个并使用内部路由进行分离。
最后可能是您误解了单点登录的概念。它的设计正是为了满足您的要求:用户登录一次即可访问多个应用程序。它不需要共享任何内容,每个唯一的客户端都会获得自己的身份验证令牌(以及可选的多个授权令牌来访问 API)。
IdentityServer中的SSO不是通过共享cookie来实现的,而是通过Client将授权委托给IdentityServer来实现的。您的客户端应用程序应使用授权端点通过 SSO 进行授权。 您还需要考虑使用authorization_code grant_type https://medium.com/oauth-2/why-you-should-stop-using-the-oauth-implicit-grant-2436ced1c926