asp.net-core - 如何使用 Sustainsys Saml2 从 Idp 发起的登录中检索声明?
问题描述
我正在尝试使用 ASP.NET Core Identity(不是 IdentityServer)向 ASP.NET Core MVC 应用程序添加对 SAML 身份验证的支持。使用 StubIdp 进行测试时,流程“有效”- SAMLResponse 已发布到/Saml2/Acs
,我被重定向到带有 Identity.External cookie 的应用程序,但我ClaimsPrincipal
的是空的且未经身份验证。即使我使用NameID
数据库中已经存在的用户,声明也是完全空的。
我还在控制台日志中看到以下内容:
Sustainsys.Saml2.AspNetCore2.Saml2Handler: Information: Successfully processed SAML response Microsoft.IdentityModel.Tokens.Saml2.Saml2Id and authenticated JohnDoe
我安装了 Sustainsys.Saml2.AspNetCore2 包,并将服务配置添加到startup.cs
如下:
services.AddAuthentication()
.AddSaml2(async options =>
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
var certificateSecret = await keyVaultClient.GetSecretAsync($"https://{Configuration["KeyVaultName"]}.vault.azure.net/", Configuration["ServiceProviderCertName"]);
var privateKeyBytes = Convert.FromBase64String(certificateSecret.Value);
options.SPOptions.EntityId = new EntityId(Configuration["BaseUrl"] + "/Saml2");
options.SPOptions.ReturnUrl = new Uri(Configuration["BaseUrl"]);
IdentityProvider idp = new IdentityProvider(
new EntityId("https://stubidp.sustainsys.com/Metadata"), options.SPOptions)
{
LoadMetadata = true,
MetadataLocation = "https://stubidp.sustainsys.com/Metadata",
AllowUnsolicitedAuthnResponse = true
};
options.IdentityProviders.Add(idp);
options.SPOptions.ServiceCertificates.Add(new X509Certificate2(privateKeyBytes));
});
Configuration["BaseUrl"]
是我的应用程序的基本 URL,在本例中是 localhost 端口。
我显然错过了一些东西,但我不知道是什么。我是否需要以某种方式将 Saml2 服务显式连接/映射到 ASP.NET Core Identity?
解决方案
能够根据此 GitHub 问题中的评论解决此问题。
我的评论解释了我如何能够实施解决方法:https ://github.com/Sustainsys/Saml2/issues/1030#issuecomment-616842796
推荐阅读
- javascript - Vue-事件总线内的变量不是反应式的
- javascript - 关于如何在客户端和服务器之间使用 Websockets 的简单示例
- pdfbox - 使用 PdfBox 将 Pdf 转换为图像
- html - 如何在不改变网站响应能力的情况下增加轮播的宽度
- ios - 当我重新启动我的设备时,是否可以在没有任何用户交互的情况下连接蓝牙设备,并在 iOS 后台执行任务?
- java - 在华为中自动将文本粘贴到编辑短信中的文本
- java - 如何从另一个类调用活动的一种方法
- python - PyQT5 program returning error only during first compile run
- react-native - 节点服务器在本机反应中停止
- python - 使用 Python 从网站中提取表数据