使用 'itfoxtec-identity-saml2' 的 SAML2.0 访问令牌
SAML2.0 Access token using 'itfoxtec-identity-saml2'
我正在尝试将您的 Nuget 包用于 dotnet 核心,但我取得了一些成功,我也可以登录到 SAML 身份提供商,如 Onelogin、Okta,我也获得了登录用户信息,但我在生成访问令牌时感到困惑(用于调用 SAML 身份提供者 API 的持有者令牌)。我将如何获得该令牌?
我可以在 saml2AuthnResponse 中看到 securitytoken 对象,但不知道如何使用该令牌,并且在该对象中安全密钥和 singin 密钥为空。
我对此完全陌生,所以我可能误解了什么。
请帮帮我
[Route("AssertionConsumerService")]
public async Task<IActionResult> AssertionConsumerService()
{
var binding = new Saml2PostBinding();
var saml2AuthnResponse = new Saml2AuthnResponse(config);
binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);
if (saml2AuthnResponse.Status != Saml2StatusCodes.Success)
{
throw new AuthenticationException($"SAML Response status: {saml2AuthnResponse.Status}");
}
binding.Unbind(Request.ToGenericHttpRequest(), saml2AuthnResponse);
await saml2AuthnResponse.CreateSession(HttpContext, claimsTransform: (claimsPrincipal) => ClaimsTransform.Transform(claimsPrincipal));
var relayStateQuery = binding.GetRelayStateQuery();
var returnUrl = relayStateQuery.ContainsKey(relayStateReturnUrl) ? relayStateQuery[relayStateReturnUrl] : Url.Content("~/");
return Redirect(returnUrl);
}
您可以通过在 appsettings.json 中设置 Saml2Configuration.SaveBootstrapContext = true
来访问作为 XML 字符串的 SAML 2.0 令牌:
...
"Saml2": {
"SaveBootstrapContext": true,
"IdPMetadata": "https://localhost:44305/metadata",
"Issuer": "itfoxtec-testwebappcore",
...
}
或者您可以在代码中设置配置:
config.SaveBootstrapContext = true;
然后您可以在 saml2AuthnResponse.ClaimsIdentity.BootstrapContext
:
中读取 SAML 2.0 令牌作为 XML 字符串
public async Task<IActionResult> AssertionConsumerService()
{
var binding = new Saml2PostBinding();
var saml2AuthnResponse = new Saml2AuthnResponse(config);
binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);
if (saml2AuthnResponse.Status != Saml2StatusCodes.Success)
{
throw new AuthenticationException($"SAML Response status: {saml2AuthnResponse.Status}");
}
binding.Unbind(Request.ToGenericHttpRequest(), saml2AuthnResponse);
await saml2AuthnResponse.CreateSession(HttpContext, claimsTransform: (claimsPrincipal) => ClaimsTransform.Transform(claimsPrincipal));
var samlTokenXml = saml2AuthnResponse.ClaimsIdentity.BootstrapContext as string;
var relayStateQuery = binding.GetRelayStateQuery();
var returnUrl = relayStateQuery.ContainsKey(relayStateReturnUrl) ? relayStateQuery[relayStateReturnUrl] : Url.Content("~/");
return Redirect(returnUrl);
}
我正在尝试将您的 Nuget 包用于 dotnet 核心,但我取得了一些成功,我也可以登录到 SAML 身份提供商,如 Onelogin、Okta,我也获得了登录用户信息,但我在生成访问令牌时感到困惑(用于调用 SAML 身份提供者 API 的持有者令牌)。我将如何获得该令牌?
我可以在 saml2AuthnResponse 中看到 securitytoken 对象,但不知道如何使用该令牌,并且在该对象中安全密钥和 singin 密钥为空。
我对此完全陌生,所以我可能误解了什么。
请帮帮我
[Route("AssertionConsumerService")]
public async Task<IActionResult> AssertionConsumerService()
{
var binding = new Saml2PostBinding();
var saml2AuthnResponse = new Saml2AuthnResponse(config);
binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);
if (saml2AuthnResponse.Status != Saml2StatusCodes.Success)
{
throw new AuthenticationException($"SAML Response status: {saml2AuthnResponse.Status}");
}
binding.Unbind(Request.ToGenericHttpRequest(), saml2AuthnResponse);
await saml2AuthnResponse.CreateSession(HttpContext, claimsTransform: (claimsPrincipal) => ClaimsTransform.Transform(claimsPrincipal));
var relayStateQuery = binding.GetRelayStateQuery();
var returnUrl = relayStateQuery.ContainsKey(relayStateReturnUrl) ? relayStateQuery[relayStateReturnUrl] : Url.Content("~/");
return Redirect(returnUrl);
}
您可以通过在 appsettings.json 中设置 Saml2Configuration.SaveBootstrapContext = true
来访问作为 XML 字符串的 SAML 2.0 令牌:
...
"Saml2": {
"SaveBootstrapContext": true,
"IdPMetadata": "https://localhost:44305/metadata",
"Issuer": "itfoxtec-testwebappcore",
...
}
或者您可以在代码中设置配置:
config.SaveBootstrapContext = true;
然后您可以在 saml2AuthnResponse.ClaimsIdentity.BootstrapContext
:
public async Task<IActionResult> AssertionConsumerService()
{
var binding = new Saml2PostBinding();
var saml2AuthnResponse = new Saml2AuthnResponse(config);
binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);
if (saml2AuthnResponse.Status != Saml2StatusCodes.Success)
{
throw new AuthenticationException($"SAML Response status: {saml2AuthnResponse.Status}");
}
binding.Unbind(Request.ToGenericHttpRequest(), saml2AuthnResponse);
await saml2AuthnResponse.CreateSession(HttpContext, claimsTransform: (claimsPrincipal) => ClaimsTransform.Transform(claimsPrincipal));
var samlTokenXml = saml2AuthnResponse.ClaimsIdentity.BootstrapContext as string;
var relayStateQuery = binding.GetRelayStateQuery();
var returnUrl = relayStateQuery.ContainsKey(relayStateReturnUrl) ? relayStateQuery[relayStateReturnUrl] : Url.Content("~/");
return Redirect(returnUrl);
}