我正在使用 IdentityServer4 并配置了 OpenId Connect 提供程序。我想要做的是将用户名作为查询字符串的一部分传递给提供者,以便提供者预先填写用户名字段。我同时拥有 ADFS 和 Azure AD 提供程序,并且希望此功能能够同时使用。这可能吗?如果可能的话,如何实现?
在 ExternalController
的 Challenge
方法中,我添加了我认为应该有效的内容,但它没有执行任何操作:
[HttpGet]
public async Task<IActionResult> Challenge(string provider, string returnUrl, string user)
{
if (string.IsNullOrEmpty(returnUrl)) returnUrl = "~/";
if (Url.IsLocalUrl(returnUrl) == false && _interaction.IsValidReturnUrl(returnUrl) == false)
{
throw new Exception("invalid return URL");
}
if (AccountOptions.WindowsAuthenticationSchemeName == provider)
{
return await ProcessWindowsLoginAsync(returnUrl);
}
else
{
var props = new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(Callback)),
Items =
{
{ "returnUrl", returnUrl },
{ "scheme", provider },
{ "login_hint", user }
}
};
return Challenge(props, provider);
}
}
最佳答案
您可以使用 OnRedirectToIdentityProvider
实现您想要的目标OpenIdConnectEvents
的属性(property)类:
Invoked before redirecting to the identity provider to authenticate. This can be used to set ProtocolMessage.State that will be persisted through the authentication process. The ProtocolMessage can also be used to add or customize parameters sent to the identity provider.
您可以通过 AddOpenIdConnect
连接到此过程。函数,在 Startup.ConfigureServices
中使用 services.AddAuthentication
时调用。以下是符合您要求的示例:
services
.AddAuthentication(...)
.AddOpenIdConnect(options =>
{
...
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = ctx =>
{
if (ctx.HttpContext.Request.Query.TryGetValue("user", out var stringValues))
ctx.ProtocolMessage.LoginHint = stringValues[0];
return Task.CompletedTask;
}
};
});
其中大部分只是用于添加身份验证、OIDC 以及为上述事件注册事件处理程序的样板代码。最有趣的部分是:
if (ctx.HttpContext.Request.Query.TryGetValue("user", out var stringValues))
ctx.ProtocolMessage.LoginHint = stringValues[0];
当问题中的 Challenge
操作从查询字符串参数获取 user
时,上面的代码会读出 user
查询字符串请求中的参数(可能有多个,这就是为什么我们这里有 StringValues
)并将其设置为 LoginHint
属性(如果找到)。
注意:我已经用 https://demo.identityserver.io 对此进行了测试(当然,这是有效的)。
关于c# - 添加查询字符串参数重定向至 IdP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52297761/