【问题标题】:azuread oauth redirect_uri query parameterazuread oauth redirect_uri 查询参数
【发布时间】:2017-01-30 04:49:36
【问题描述】:

是否可以将自定义查询参数添加到 AzureAD OAuth 流的 redirect_uri?

我们已经尝试过,但是当 OAuth 流重定向回 redirect_uri 时,我们添加的任何查询参数都被删除了。我想知道是否有办法配置 AzureAD 应用程序以保留此类自定义查询参数

【问题讨论】:

    标签: oauth azure-active-directory


    【解决方案1】:

    是否可以将自定义查询参数添加到 AzureAD OAuth 流的 redirect_uri?

    是的,如果您将 Azure AD 与 OWIN 集成,则可以轻松添加自定义查询参数。这个问题也讨论了here,这是一个代码示例供您参考:

    在 Startup.Auth.cs 中,设置 OpenIdConnectAuthenticationOptions,如下所示:

    app.UseOpenIdConnectAuthentication(
      new OpenIdConnectAuthenticationOptions
      {
        //...
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
          RedirectToIdentityProvider = OnRedirectToIdentityProvider,
          MessageReceived = OnMessageReceived
        },
      });
    

    使用 RedirectToIdentityProvider 注入自定义参数:

    private Task OnRedirectToIdentityProvider(RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
    {
      var stateQueryString = notification.ProtocolMessage.State.Split('=');
      var protectedState = stateQueryString[1];
      var state = notification.Options.StateDataFormat.Unprotect(protectedState);
      state.Dictionary.Add("mycustomparameter", "myvalue");
      notification.ProtocolMessage.State = stateQueryString[0] + "=" + notification.Options.StateDataFormat.Protect(state);
      return Task.FromResult(0);
    }
    

    然后使用 MessageReceived 来提取它:

    private Task OnMessageReceived(MessageReceivedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
    {
      string mycustomparameter;
      var protectedState = notification.ProtocolMessage.State.Split('=')[1];
      var state = notification.Options.StateDataFormat.Unprotect(protectedState);
      state.Dictionary.TryGetValue("mycustomparameter", out mycustomparameter);
      return Task.FromResult(0);
    }
    

    【讨论】:

    • 感谢您的回复。我可以看出我有点不清楚。我们有一个参数希望在 redirect_uri 上返回,而不是作为状态的一部分,因此我们可以在检查 id_token 和状态之前决定执行哪个逻辑分支,但是我们直接添加到的任何动态查询参数当经过身份验证的请求被重定向到 request_uri 时,redirect_uri 被剥离。我希望这是有道理的。
    • 目前,Azure AD 不支持将客户 URL 参数从请求传输到 Web 应用。一旦您的服务器在 OWIN 验证令牌之前从身份数据提供者那里获得消息,OnMessageReceived 就会被触发。有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2014-09-14
    相关资源
    最近更新 更多