我们使用 Sustainsys 中间件和 .NET Core 连接到 SAML2 IDP。效果很好。
但是,当我们在 Startup.cs 中添加多个 IDP 时,我们就会遇到麻烦。用户将选择要登录的 IDP,然后代码应向该 IDP 发送质询。
我们如何在代码中指定哪个IDP?
使用标准 .NET Framework 非常简单:
Context.GetOwinContext().Environment.Add("saml2.idp", new Entity(IDP2EntityId));
但是 .NET Core 中间件中没有这样的构造。
这是我的代码。基本上我在启动期间添加了两个 IDP,但我不知道如何在登录/挑战期间指定哪一个?使用此代码,IDP-1 始终被选中,因为它是第一个添加的。
启动.CS
public void ConfigureServices(IServiceCollection services)
{
var authenticationBuilder = GetAuthenticationBuilder(services);
string authenticationScheme = "saml2.idp"
authenticationBuilder.AddSaml2(authenticationScheme, options =>
{
options.SPOptions = GetSPOptions();
// Add IDP-1
options.IdentityProviders.Add(
new IdentityProvider(new EntityId(IDPEntityUrl1), options.SPOptions)
{
MetadataLocation = IDPMetadataUrl1
});
// Add IDP-2
options.IdentityProviders.Add(
new IdentityProvider(new EntityId(IDPEntityUrl2), options.SPOptions)
{
MetadataLocation = IDPMetadataUrl2
});
}
}
登录控制器.CS
string saml2AuthenticationScheme = "saml2.idp";
var props = new AuthenticationProperties
{
RedirectUri = returnUrl,
Items = { { "scheme", saml2AuthenticationScheme } }
};
return Challenge(properties: props, saml2AuthenticationScheme);
如何指定在
LoginController
中使用哪个 IDP?
我找到了解决方案。我们研究了 Sustainsys 代码,发现未记录的 (?) 功能可在 AuthenticationProperties.Items 中使用“idp”项指定 IDP。像这样:
登录控制器.cs
string saml2AuthenticationScheme = "saml2.idp";
var props = new AuthenticationProperties
{
RedirectUri = returnUrl,
Items = { { "scheme", saml2AuthenticationScheme }, { "idp", theSelectedIDPIdentityId } }
};
return Challenge(properties: props, saml2AuthenticationScheme);
谢谢你的回答。我提前提出这个问题,询问是否可以在.net(而不是netcore)中做同样的事情。我在startup.Auth.cs->configureAuth 中找不到使用“authenticationBuilder.AddSaml2(authenticationScheme,...)”的选项。 预先感谢!