2017-01-30 76 views
0

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

我们已经尝试过,但是当OAuth流重定向回redirect_uri时,我们添加的任何查询参数都已被剥离。我想知道是否有一种方法来配置AzureAD应用程序来保持这种自定义查询参数

回答

0

是否可以将自定义查询参数添加到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); 
} 
+0

感谢您的回复。我可以看到我有点不清楚。 我们有一个参数,宁愿在redirect_uri上返回,而不是作为状态的一部分,因此我们可以在检查id_token和状态之前决定执行哪个逻辑分支,但是我们直接添加的任何动态查询参数到redirect_uri被删除当验证的请求被重定向到request_uri。 我希望这是有道理的。 –

+0

目前,Azure AD不支持将客户网址参数从请求传输到网络应用。一旦你的服务器从OWIN验证令牌之前的身份数据提供者获得消息,'OnMessageReceived'将被触发。它有帮助吗? –