使用 Keycloak 作为 ASP.Net 应用程序的单一登录解决方案

Posted

技术标签:

【中文标题】使用 Keycloak 作为 ASP.Net 应用程序的单一登录解决方案【英文标题】:Using Keycloak as a Single Sign On solution for ASP.Net applications 【发布时间】:2018-02-26 01:37:44 【问题描述】:

我正在尝试将 Keycloak 用作多个 ASP.Net 应用程序的单点登录服务器。我在 Github 上找到了一个名为 KeycloakOwinAuthentication 的库,我克隆了两次内提供的示例代码,以用作两个不同的应用程序。

我在 Keycloak 的同一个领域中配置了 2 个应用程序(App1 和 App2),创建了具有所有角色的测试用户并尝试登录。

预期行为:从应用程序一登录,刷新应用程序2,您将自动登录

实际结果:从 App 1 登录,刷新 App 2 并得到“'/' 应用程序中的服务器错误。如果我从 App1 注销并尝试刷新 App2,它会恢复正常!

可以找到我的 2 个示例应用程序 here... 我的受保护页面的 ActionResult(在视图中显示令牌)如下所示:

 [Authorize]
    public ActionResult About()
    
        ViewBag.Message = "Your application description page.";
        var userPrincipal = User as ClaimsPrincipal;
        ViewBag.Something = userPrincipal.Identity.ToString();
        return View(userPrincipal);

    

我的启动页面如下所示:

public class Startup

    public void Configuration(IAppBuilder app)
    
        const string persistentAuthType = "Keycloak_Cookies";

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        
            AuthenticationType = persistentAuthType
        );

        app.SetDefaultSignInAsAuthenticationType(persistentAuthType);

        app.UseKeycloakAuthentication(new KeycloakAuthenticationOptions
        
            Realm = "MyRealm",
            ClientId = "App3",
            ClientSecret = "KeycloakClientSecret",
            KeycloakUrl = "http://localhost:8080/auth",
            SignInAsAuthenticationType = persistentAuthType
        );

是否有我遗漏的特定配置?我使用了我的测试领域以及具有 3 个不同应用程序(不是 Asp)的工作领域,但我未能在所有客户端上登录。我使用的是带有两个选项卡的相同浏览器,以确保所有 cookie 都可以访问......

错误文本:

Server Error in '/' Application.
IDX10214: Audience validation failed. Audiences: 'App1'. Did not match:  validationParameters.ValidAudience: 'null' or validationParameters.ValidAudiences: 'null, App2'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed. Audiences: 'App1'. Did not match:  validationParameters.ValidAudience: 'null' or validationParameters.ValidAudiences: 'null, App2'
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

【问题讨论】:

您需要在此处发布您的示例代码,而不是任何明天可以更改或消失的第三方网站:minimal reproducible example 【参考方案1】:

我深入研究了整个库的代码,发现有一个标志禁用了受众检查。它应该在 Startup.cs 文件的 Keycloak 配置中设置。 DisableAudienceValidation 标志默认为 false,但通过将其添加到配置并将其值设置为 true,将跳过受众验证。

            app.UseKeycloakAuthentication(new KeycloakAuthenticationOptions
        
            Realm = "DotNetApps",
            ClientId = "TestingApp",
            ClientSecret = "Client_Secret_Goes_Here",
            KeycloakUrl = "http://localhost:8080/auth",
            SignInAsAuthenticationType = persistentAuthType,
            DisableAudienceValidation = true

        );

【讨论】:

【参考方案2】:

较新版本的 Keycloak 服务器 (>=6.x) 更改了在访问令牌中设置受众声明 (“aud”) 的方式。库 (Owin.Security.Keyclaok-3) 假定 Keycloak 客户端 ID 在“aud”声明中,默认情况下不再是这种情况。

修复它的一种方法是在 Keycloak 管理 UI 中,将映射器添加到正在使用的 Keycloak 客户端中。

名称:观众 映射器类型:受众 包含的客户受众:[客户 ID] 另外,确保 .NET 库配置了选项

DisableAllRefreshTokenValidation = true

startup.cs 中,这是较新的 Keycloak 服务器版本所必需的(无论如何,刷新令牌的验证都是在服务器端完成的)。

【讨论】:

以上是关于使用 Keycloak 作为 ASP.Net 应用程序的单一登录解决方案的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core 的 Keycloak 客户端

ASP.NET Core 搭载 Envoy 实现微服务身份认证(JWT)

ASP.NET Core 搭载 Envoy 实现微服务身份认证(JWT)

如何在 ASP.NET Core 中转储解析的 JWT 令牌?

使用 OrchardCMS、Umbraco 或 DotNetNuke 作为 ASP.NET 应用程序中的组件

新的 MVC 4 项目基于使用 Subsonic 作为 ORM 的 ASP.net 网络论坛应用程序......从哪里开始......?