【问题标题】:WIF: ID1014: The signature is not valid. The data may have been tampered withWIF:ID1014:签名无效。数据可能已被篡改
【发布时间】:2013-03-09 06:52:15
【问题描述】:

我们基于 Windows Identity Foundation 构建了一个依赖方应用程序。我们按照 Vittorio 书中的建议创建了一组自定义 cookie 转换,以使用 RSA 加密/签署令牌。

private void OnServiceConfigurationCreated( object sender, ServiceConfigurationCreatedEventArgs e )
{
    List<CookieTransform> sessionTransforms = new List<CookieTransform>( new CookieTransform[]
    {
        new DeflateCookieTransform(),
        new RsaEncryptionCookieTransform( e.ServiceConfiguration.ServiceCertificate ),
        new RsaSignatureCookieTransform( e.ServiceConfiguration.ServiceCertificate )
    } );

    SessionSecurityTokenHandler sessionHandler =
        new SessionSecurityTokenHandler( sessionTransforms.AsReadOnly() );

    e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace( sessionHandler );
}

我们在 web.config 中配置了一个。

<microsoft.identityModel>
  <service>
    <serviceCertificate>
      <certificateReference x509FindType="FindByThumbprint" findValue="C7FD338059CCB374798923A915BC91B718814A8E" storeLocation="LocalMachine" storeName="TrustedPeople" />
    </serviceCertificate>
  </service>
</microsoft.identityModel>

我知道 OnServiceConfigurationCreated 中的代码正在执行,因为如果我将垃圾指纹值放入配置文件,则 OnServiceConfigurationCreated 会引发异常。

不幸的是,我们的日志中经常出现以下异常。

System.Security.Cryptography.CryptographicException: ID1014: The signature is not valid. The data may have been tampered with.
at Microsoft.IdentityModel.Web.RsaSignatureCookieTransform.Decode(Byte[] encoded)
at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound)
at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)
at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver)
at Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie)
at Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken)
at Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

我们认为此异常会导致系统出现其他问题,但无法弄清楚它发生的原因。我们有三台 Web 服务器,并且我们三重检查了它们都配置为使用相同的证书指纹,并且证书安装在所有三台服务器上的相同位置。

我们还使用自定义 SessionAuthenticationModule 来处理滑动会话过期。我认为也许当该代码(如下)重新发布 cookie 时,它​​可能使用了不同的加密/签名方法,但我很确定我已经对其进行了测试,但这似乎不是案子。我包含它只是为了充分披露。

void CustomSessionAuthenticationModule_SessionSecurityTokenReceived( object sender, SessionSecurityTokenReceivedEventArgs e )
{
    DateTime now = DateTime.UtcNow;
    DateTime validFrom = e.SessionToken.ValidFrom;
    DateTime validTo = e.SessionToken.ValidTo;

    double tokenLifetime = (validTo - validFrom).TotalMinutes;

    SessionAuthenticationModule sam = sender as SessionAuthenticationModule;

    if( now < validTo && now > validFrom.AddMinutes( tokenLifetime / 2 ) )
    {
        e.SessionToken = sam.CreateSessionSecurityToken(
            e.SessionToken.ClaimsPrincipal, e.SessionToken.Context,
            now, now.AddMinutes( tokenLifetime ), e.SessionToken.IsPersistent );
        e.ReissueCookie = true;
    }
}

据我们所知,我们已经完成了 docs/blogs/etc 所说的一切,但我们仍然遇到此异常。在这一点上,任何提示/指针/有根据的猜测都会有所帮助。

【问题讨论】:

    标签: wif


    【解决方案1】:

    您可能想要检查您的应用程序设置的 cookie 数据的总大小。如果您包含大量声明,除非您使用会话模式,否则 cookie 会相应增长。例如。 Safari 对总 cookie 数据大小有 4K 限制。如果您打破此限制,您将开始丢失 cookie,这可能意味着您将丢失带有部分签名的 cookie。

    附带说明,如果您可以迁移到 WIF 4.5,您可以选择使用 MachineKeySessionSecurityTokenHandler,而不是执行基于证书的 cookie 加密。

    【讨论】:

    • 我们实际上已经实现了 Thinktecture 的 MachineKeySessionSecurityTokenHandler 来尝试解决这个问题。但是,如果是 cookie 大小问题,我们可能仍然会遇到同样的问题。我们只有几个声明,FedAuth cookie 只分为两部分(FedAuth 和 FedAuth1),但我会在周一回到办公室时进一步深入研究。 WIF 4.5 文档似乎暗示,如果我实现 MachineKey 处理程序,我需要创建自己的缓存引擎,但从您的评论和更多阅读来看,似乎只有我使用会话模式。
    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2016-06-24
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多