【问题标题】:OpenId Connect authentication for UI and service access用于 UI 和服务访问的 OpenId Connect 身份验证
【发布时间】:2015-11-15 06:06:46
【问题描述】:

我们正在构建一个应用程序,它使用IdentityServer3 OpenID Connect authentication 来保护它的RESTful APIs

对于UI 访问,我们使用在IS3 注册的隐式流客户端。我们的 Startup 类设置OIDC Owin Security module 连接到IS3。一切都很好。

此应用程序的下一个要求是我们的REST API 不仅可以通过网络UI 访问,还可以通过其他服务访问。客户端凭据流似乎符合要求。关键是让服务通过重定向到IS3 登录页面并在那里填写用户名/密码来联系我们的REST API 是不切实际的。 Client Credential flow 客户端直接在IS3 服务器上获取令牌,然后对我们的REST API 执行其操作。

Startup 类如下所示:

internal class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // The Bearer token authentication is used for service to service
        // REST API access (also for testing purposes)
        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = ConfigurationManager.AppSettings["IdmUrl"],
            NameClaimType = "client_id",
            RoleClaimType = "client_role"
            // ...
        });

        // OIDC authentication is used for UI access
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            Authority = ConfigurationManager.AppSettings["IdmUrl"],
            // ...
        }
    }
}

它在这两种情况下都能很好地工作,人类通过浏览器访问它,从而使用隐式流以及使用客户端凭据流的服务。但是,如果服务在客户端凭据流中使用的令牌过期,请求将再次重定向到 IS3 服务器。我们希望它返回401 error,以便客户端知道它需要再次获取新令牌。

你能想出一种方法来结合这两个流程,以便它们很好地结合在一起吗?

【问题讨论】:

    标签: authentication oauth-2.0 owin openid-connect identityserver3


    【解决方案1】:

    要允许 401 未经授权但也允许带有 UI 的 302 重定向,您需要实现 solution like the one here(来自 Identity Server 的作者之一)。

    在这种情况下,通过添加 AJAX 检测来调整 app.UseCookieAuthentication() 设置,并且仅在检测到 AJAX 请求时重定向到登录页面。您应该能够根据自己的需要进行调整。

    【讨论】:

    • 感谢您的提示,斯科特。我使用逻辑来阻止重定向并将其放在OIDC RedirectToIdentityProvider 通知中。它似乎有效。
    猜你喜欢
    • 2023-03-27
    • 2021-09-07
    • 2017-12-17
    • 2016-06-28
    • 2020-05-28
    • 2019-03-14
    • 1970-01-01
    • 2018-07-01
    • 2010-09-28
    相关资源
    最近更新 更多