2013-03-06 87 views
0

我们已经构建了一个基于Windows Identity Foundation的依赖方应用程序。我们遵循Vittorio的书中的建议,并创建了一组自定义的cookie转换,以使用RSA对令牌进行加密/签名。WIF:ID1014:签名无效。数据可能已被篡改

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; 
    } 
} 

从我们可以告诉我们所做的一切文档/博客/等纷纷表示,但我们仍然得到此异常。任何提示/指针/受过教育的猜测都会对此有所帮助。

回答

0

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

请注意,如果您可以转移到WIF 4.5,您可以选择使用MachineKeySessionSecurityTokenHandler而不是进行基于证书的cookie加密。

+0

我们实际上已经从Thinktecture实现了MachineKeySessionSecurityTokenHandler来试图解决这个问题。但是,如果这是一个cookie大小问题,我们可能仍然会遇到同样的问题。我们只有几个说法,而FedAuth cookie只分为两部分(FedAuth和FedAuth1),但我会在周一回到办公室的时候深入一点。 WIF 4.5文档似乎意味着如果我实现MachineKey处理程序,我需要创建自己的缓存引擎,但是从您的评论和更多阅读中,似乎只有在使用会话模式时才是如此。 – 2013-03-09 15:49:19

相关问题