【问题标题】:How to use end client state parameter in IdentityServer 3?如何在 IdentityServer 3 中使用终端客户端状态参数?
【发布时间】:2021-12-11 09:19:46
【问题描述】:

我已将 IdentityServer 3 配置为使用指向 AAD 的外部 IdentityProvider

截至目前,当我向IdentityServer 发送请求时,我被正确重定向到 AAD 进行登录,但是,我发送到 IdentityServer 的“状态”参数被覆盖,OpenIdConnect.AuthenticationProperties 的值被加密并作为查询字符串中的状态发送到 AAD。

例如:

https://localhost:44333/idpaad/connect/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&redirect_uri=https://localhost:44394/&response_mode=query&response_type=code&scope=openid%20email&state=9b0e82c3-e623-42f1-bede-493243c103e7

这里, https://localhost:44333/idpaad/connect/authorize -> IdentityServer 端点 state=9b0e82c3-e623-42f1-bede-493243c103e7 -> 客户端生成的 GUID 作为查询字符串发送。

当我在 IdentityServer OpenIdConnectAuthenticationNotifications 的 StartUp.cs 中看到“RedirectToIdentityProvider”中间件时,状态的值更新为 OpenIdConnect.AuthenticationProperties=(protected values) 而不是 GUID,并且它也作为查询字符串返回给重定向 URI。 enter image description here 有没有办法发送原始状态而不被 IdentityServer3 覆盖?

在使用 wsFederation 时,我没有遇到此问题,并且该问题直接转发给 IdP。

非常感谢任何帮助。

【问题讨论】:

    标签: asp.net authentication azure-active-directory openid-connect identityserver3


    【解决方案1】:

    大多数情况下,建议 Azure Active Directory 集成应用程序在向 Azure AD 发送登录请求时保持应用程序状态。 推荐的实现方式是使用 OpenID Connect 标准中定义的“状态”参数

    如果您查看这个document 表单OpenID,您会发现使用state 参数的主要原因是为了缓解CSRF 攻击

    推荐。用于维护请求和回调之间状态的不透明值。通常,跨站点请求伪造(CSRF、XSRF)缓解是通过将此参数的值与浏览器 cookie 加密绑定来完成的。

    “state”参数用于防止跨站请求伪造攻击和在身份验证请求发生之前维护用户的状态

    在使用 OpenID Connect OWIN 中间件ASP.NET or ASP.NET CORE Web 应用程序中,‘state’ parameter 在发送身份验证请求时由中间件自动维护,这是您看到 state 参数在您的情况下被覆盖的唯一原因。

    但如果您愿意,您可以在 state 参数中添加自定义数据。在 OpenIdConnectNotifications 的 RedirectToIdentityProvider 事件中使用以下代码将自定义数据注入“状态”参数。

    var stateQueryString = notification.ProtocolMessage.State.Split('=');
    var protectedState = stateQueryString[1];
    var state = notification.Options.StateDataFormat.Unprotect(protectedState);
    state.Dictionary.Add("MyData","123");
    notification.ProtocolMessage.State = stateQueryString[0] + "=" + notification.Options.StateDataFormat.Protect(state);
    

    查看documentMicrosoft identity platform and OpenID Connect protocol 了解详细信息。

    【讨论】:

    • 感谢您的详细解释。我有一个通过此端点调用身份服务器的客户端,但是,当我遇到 RedirectToIdentityProvider 事件时,发送的数据不会被维护并且会丢失。我想完全按照您的建议进行操作,但是如何将“状态”信息传递给该事件?不幸的是,现在这对我来说是一个障碍,任何指示都会有所帮助。
    • 你检查过@Saca 的answer。您还可以向 Microsoft 支持提出请求以获取更多帮助。
    • 是的,我确实看过答案,不幸的是,它没有解决我的目的。这将讨论从 AAD 返回到中间件时如何使用这些值。我想要一种将客户端查询字符串信息提取到中间件中的机制。我在 Microsoft 支持部门发布了一张票。谢谢大家的帮助!!
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2016-07-20
    • 1970-01-01
    • 2020-08-30
    • 2018-09-14
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多