【发布时间】:2017-01-30 04:49:36
【问题描述】:
是否可以将自定义查询参数添加到 AzureAD OAuth 流的 redirect_uri?
我们已经尝试过,但是当 OAuth 流重定向回 redirect_uri 时,我们添加的任何查询参数都被删除了。我想知道是否有办法配置 AzureAD 应用程序以保留此类自定义查询参数
【问题讨论】:
标签: oauth azure-active-directory
是否可以将自定义查询参数添加到 AzureAD OAuth 流的 redirect_uri?
我们已经尝试过,但是当 OAuth 流重定向回 redirect_uri 时,我们添加的任何查询参数都被删除了。我想知道是否有办法配置 AzureAD 应用程序以保留此类自定义查询参数
【问题讨论】:
标签: oauth azure-active-directory
是否可以将自定义查询参数添加到 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);
}
【讨论】:
OnMessageReceived 就会被触发。有用吗?