首页 > 解决方案 > Null : AssertionConsumerService => saml2AuthnResponse.CreateSession 期间的对象引用

问题描述

我在 MVC.NET(4.7.2 框架)中运行 ITFoxtec.Identity.Saml2.Mvc(v4.5.0)。而且我的 IdP 说已成功通过身份验证...正在重定向回来...但是当我从 IdP 收到响应时-我在此调用中得到了一个空异常。不幸的是,它没有给我一个行号或任何有助于追踪它的东西。

saml2AuthnResponse.CreateSession()

我尝试了多种构建 nuget 包的方法,以使其在失败的地方注销 - 但到目前为止,在从包外部使用/引用时没有运气。我在 web.config 中设置了如下其他设置:

<add key="Saml2:CertificateValidationMode" value="PeerOrChainTrust" />
<add key="Saml2:RevocationMode" value="NoCheck" />

我的 AssertionConsumerService 与您网站上的示例几乎相同:

public ActionResult AssertionConsumerService()
{       
    var binding = new Saml2PostBinding();
    var saml2AuthnResponse = new Saml2AuthnResponse(config);

    binding.Unbind(Request.ToGenericHttpRequest(), saml2AuthnResponse);
    saml2AuthnResponse.CreateSession(claimsAuthenticationManager: new SAMLDefaultClaimsAuthenticationManager());

    var returnUrl = binding.GetRelayStateQuery()[relayStateReturnUrl];
    return Redirect(string.IsNullOrWhiteSpace(returnUrl) ? Url.Content("~/") : returnUrl);
}

在 Saml2ResponseExtensions.cs CreateSession() ln.19 上的 printf 调试中,我可以说它(可能?)不会在这一行之前抛出(因为我可以复制+粘贴在我的外部范围内重新创建所有前面的变量,没有问题)。

var sessionSecurityToken = lifetime.HasValue 
    ? new SessionSecurityToken(transformedPrincipal, lifetime.Value) 
    ...

@AndersRevsgaard 有什么想法吗?

标签: nullsaml-2.0itfoxtec-identity-saml2

解决方案


我跟踪到这一行,这会引发 Null ref 错误:

FederatedAuthentication.SessionAuthenticationModule.AuthenticateSessionSecurityToken(sessionSecurityToken, true);

虽然仍然很神秘,但这个答案解决了这个问题: 是什么让 FederatedAuthentication.SessionAuthenticationModule 返回 NULL? 通过向 web.config 添加一些部分。

<configSections>
  <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
  <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</configSections>
<system.web>
  <authentication mode="None" />
</system.web>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </modules>
</system.webServer>
<system.identityModel.services>
  <federationConfiguration>
    <cookieHandler requireSsl="false" />
  </federationConfiguration>
</system.identityModel.services>

您可以查看 ITFoxTec 测试 web.config 以获取完整示例: https ://github.com/ITfoxtec/ITfoxtec.SAML2/blob/master/WebAppTest/Web.config


推荐阅读