码迷,mamicode.com
首页 > 其他好文 > 详细

使用WIF实现单点登录Part III —— 常见问题

时间:2014-04-30 15:37:00      阅读:699      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   img   code   size   log   c   tar   t   

bubuko.com,布布扣

InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException
(有关详细信息,请参见内部异常)。如果使用的是 IIS 7.5,则这可能是由于应用程序池的 loadUserProfile 设置设成了 false。]

1、先说下这个问题
  由于每个实例都有一个不同的密钥,因此默认的 cookie 加密机制(使用数据保护应用程序编程接口 (DPAPI))并不合适。这意味着由一个 Web 角色实例创建的 cookie 可能无法被另一个 Web 角色实例读取。这可能会导致服务失败,从而有效导致拒绝服务。若要解决此问题,采用的 cookie 加密机制应使用由所有 Web 角色实例共享的密钥。

使用
Framework3.5+ microsoft.identityModel 解决方式

config 增加
      <serviceCertificate>
        <certificateReference x509FindType="FindByThumbprint" findValue="9416A5EF21AE240864BEAB922961EEF546D66B23"/>
      </serviceCertificate>

然后在 Global.asax
 Application_Start() 中 增加事件
FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;


      /// <summary>
        /// By default, WIF uses DPAPI to encrypt token.
        /// But DPAPI is not supported in Windows Azure.
        /// So we use a certificate instead.
        /// </summary>
        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);
        }

使用 Framework4.5 + system.identityModel   解决方式 与 上面处理方式差不多 只要是在网站运行时通过反射替换使用相同证书


        void  void FederatedAuthentication_FederationConfigurationCreated(object sender, System.IdentityModel.Services.Configuration.FederationConfigurationCreatedEventArgs e)
        {
            List<CookieTransform> sessionTransforms = new List<CookieTransform>(new CookieTransform[]
            {
                new DeflateCookieTransform(),
                new RsaEncryptionCookieTransform(e.FederationConfiguration.ServiceCertificate),
                new RsaSignatureCookieTransform(e.FederationConfiguration.ServiceCertificate)
            });
            SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
            e.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
        }

参考:
http://msdn.microsoft.com/zh-cn/library/gg185962.aspx
http://msdn.microsoft.com/zh-cn/library/ee517293.aspx
http://code.msdn.microsoft.com/MultiTenant-Web-Application-bed11640/view/SourceCode#content
实际运行中,使用 Framework4.5 + system.identityModel   和 使用 Framework3.5+ microsoft.identityModel  方式之间是不能够单点登录的
会出“System.Xml.XmlException: 未正确格式化输入源 ” 错误



2、多服务器部署

首先需要保证部署的服务器使用的是同一个证书,证书的创建导入导出这里就不详细介绍了,

说一个常见问题


bubuko.com,布布扣
问题原因已经提示的很清楚了“应用程序池的权限不够”

解决方式是:在应用程序池-》高级-》表示 选择LocalSystem
bubuko.com,布布扣

Demo: http://pan.baidu.com/s/1i3okYlZ

 

使用WIF实现单点登录Part III —— 常见问题,布布扣,bubuko.com

使用WIF实现单点登录Part III —— 常见问题

标签:com   http   blog   style   img   code   size   log   c   tar   t   

原文地址:http://www.cnblogs.com/shanhe/p/3699493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!