在开发基于声明的身份验证应用程序时,为了使应用程序在部署到负载平衡服务器时正常工作,我跟着http://msdn.microsoft.com/en-us/library/ff803371.aspx通过使用RsaTokenTransform处理程序替换默认的SecurityTokenHandler来使用a加密会话cookie自备证书:
X509Certificate2 serviceCertificate = new X509Certificate2(certificate, certificatePassword);
List<CookieTransform> sessionTransformers =
new List<CookieTransform>
(
new CookieTransform[]
{
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(serviceCertificate),
new RsaSignatureCookieTransform(serviceCertificate)
}
);
SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransformers.AsReadOnly());
FederatedAuthentication.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
为了使应用程序更加灵活和可移植,证书将从数据库中提取并在运行时存储在X509Certificate2对象中。
这适用于托管应用程序的一台服务器,但是一旦我切换到服务器场,我偶尔会收到错误:
ID1014:签名无效。数据可能已被篡改
由于同样的问题,一些javascript,css和字体文件也无法加载。
相关问题但未解决问题:
WIF- ID1014: The signature is not valid. The data may have been tampered with
WIF: ID1014: The signature is not valid. The data may have been tampered with
答案 0 :(得分:4)
在互联网上搜索后,我发现没有明确的解释或解决方案;为了节省人们可能遇到与我相同问题的时间,这里有解释:
问题的原因是默认情况下,X509Certificate2对象仅提供临时私钥提供程序,该提供程序不会在服务器场中的不同服务器之间保留。一旦请求从原始服务器转到另一个服务器,私钥供应商就会在该服务器上变空(因为它不会持久存在),因此抛出异常,因为签名验证需要证书的私钥。
要解决此问题,只需在创建X509Certificate2对象时使Privatekey Provider持久化:
X509Certificate2 serviceCertificate = new X509Certificate2(certificate, certificatePassword, X509KeyStorageFlags.MachineKeySet|X509KeyStorageFlags.PersistKeySet);