.NET Core 2.1 Linux Keycloak integration\authentication(OpenID Connect + SSSD)

.NET Core 2.1 Linux Keycloak integration\authentication (OpenID Connect + SSSD)

我现在有点迷茫了,我想在这里实现集中的用户管理,我们在 Linux 上托管了 .NET Core 2.1 Web MVC 应用程序,它使用 Identity.EntityFrameworkCore 来存储用户信息.我们还有用于用户管理的 FreeIPA,它基本上是 LDAP 目录上的 Kerberos 身份验证。

因为它部署在 Linux 我不能使用 WindowsAuthentication 来使用 Kerberos,而且没有 Windows 兼容包就没有使用 LDAP 的标准方法。剩下的唯一方法是使用第三方库,但不幸的是我对 .NET Core 和 C# 真的很陌生,所以这就是我正在尝试做的事情:

  1. 在LDAP中存储用户信息(roles\claims\credentials)以实现集中用户管理(现在使用数据库表)
  2. 我想实施 SSO,这可以通过 Kerberos 协商身份验证实现,但现在我找不到在 Linux 上使用它的方法。 app-> redirect to Keycloak -> Kerberos->OpenID Connect -> app 也有可能,但我很高兴找到 Kerberos
  3. 的解决方案

好吧,我发现了一堆第三方库,比如 this, this and the whole bunch of THIS,它们看起来都很有前途,但我没有找到任何具有足够文档或示例的库来与 .NET Core 一起使用 Identity.

正如我所说,我是 C# 和 .NET 的新手,不幸的是,我不知道这个库中的哪个更好,或者是否有任何直接使用它们的方法存储和验证用户。

P.S 我知道这个问题很愚蠢,但出于学习目的我不得不提出一个。如有任何帮助,我们将不胜感激!

好吧,看起来我基本上找到了答案,但它还是有一些注意事项。我将 Keycloak OpenID Connect 设置与 SSSD 联合一起使用,因为无论如何 LDAP 对我的目标来说都是错误的方法。有关该设置的更多信息,您可以阅读 here, I faced some stupid moments over there, most of them are predicted and bypassed in this 指南,但那更像是一个 FreeIPA + Keycloak 线程。最后应该注意的 Keycloak 事情:我必须添加并允许 HBAC "keycloak" 服务才能使其工作,否则我的 SSSD 身份验证将被拒绝。转到 .NET Core 部分:我的应用程序是 2.1,我的设置如下所示:

我将以下内容添加到 Startup.cs 依赖项中:

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using System.IdentityModel.Tokens;

接下来,我将配置添加到appsettings.json

"Oidc": {
"ClientId": "<your client id here>",
"ClientSecret": "<your secret here>",
"Authority": "https://<ip:port of keycloak https>/auth/realms/<realm name>",
"ResponseType": "code"
}

好的,前往配置本身:

services.AddAuthentication(options => {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
            .AddCookie("Cookies")
            .AddOpenIdConnect(options =>
            {
                options.ClientId = Configuration["Oidc:ClientId"];
                options.ClientSecret = Configuration["Oidc:ClientSecret"];
                options.Authority = Configuration["Oidc:Authority"];
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;
                options.ResponseType = Configuration["Oidc:ResponseType"];
                options.Scope.Add("claims");
                options.ClaimActions.Clear();
                options.ClaimActions.MapUniqueJsonKey("roles", "roles");
            }
        );;

我希望这个简短的回答能帮助人们设置 FreeIPA+Keycloak 并将其连接到 .NET Core,因为我为此耗费了一个星期:D。