zoukankan      html  css  js  c++  java
  • 使用WIF实现单点登录Part IV —— 常见问题

    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、多服务器部署

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

    说一个常见问题



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

    解决方式是:在应用程序池-》高级-》表示 选择LocalSystem


    Demo: http://pan.baidu.com/s/1i3okYlZ
  • 相关阅读:
    亲测——pycharm下运行第一个scrapy项目 ©seven_clear
    [转]pycharm的一些快捷键
    一个豆瓣API的使用——拒绝思维定式
    小试牛刀--利用豆瓣API爬取豆瓣电影top250
    Python GUI编程--Tkinter
    多线程详解
    K8S+GitLab-自动化分布式部署ASP.NET Core(一) 部署环境
    初学者浅度剖析eShopOnContainers 里面用到的MediatR .
    通过Task异步加快对数组的运算
    为什么是容器?
  • 原文地址:https://www.cnblogs.com/shanhe/p/3699493.html
Copyright © 2011-2022 走看看