【问题标题】:WSFederationAuthenticationModule.RedirectingToIdentityProvider event is not called未调用 WSFederationAuthenticationModule.RedirectingToIdentityProvider 事件
【发布时间】:2015-01-28 19:46:01
【问题描述】:

我的 Global.asax.cs 文件中有 2 个事件

WSFederationAuthenticationModule_SecurityTokenValidatedWSFederationAuthenticationModule_RedirectingToIdentityProvider

wif 引擎不调用 WSFederationAuthenticationModule_RedirectingToIdentityProvider。为什么?

public class MvcApplication : System.Web.HttpApplication
{ 
    void WSFederationAuthenticationModule_SecurityTokenValidated(object sender, SecurityTokenValidatedEventArgs e)
    {
        FederatedAuthentication.SessionAuthenticationModule.IsSessionMode = true;
    }


    void WSFederationAuthenticationModule_RedirectingToIdentityProvider(object sender, RedirectingToIdentityProviderEventArgs e)
    {
        //some code
    }
}

这是 web.config 中的 microsoft.identityModel 部分

<microsoft.identityModel>
        <service saveBootstrapTokens="true">
          <audienceUris mode="Never">

          </audienceUris>
          <federatedAuthentication>
            <wsFederation passiveRedirectEnabled="true" issuer="http://localhost/dss.web.sts.tokenbaker/" realm="http://localhost/dss.web.frontend" requireHttps="false" />
            <cookieHandler requireSsl="false" />



          </federatedAuthentication>

          <issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <trustedIssuers>
              <add thumbprint="308efdee6453fff68c402e5eceee5b8bb9eaa619" name="servcert" />

            </trustedIssuers>
          </issuerNameRegistry>
        </service>
      </microsoft.identityModel>

【问题讨论】:

  • 您是否检查过 web.config 中 元素上的passiveRedirectEnabled 属性是否设置为true?
  • 您可以发布您的 web.config 的 microsoft.identityModel 部分吗?这应该有助于诊断情况。
  • 我添加了 microsoft.identityModel 部分的内容
  • 你有没有想过这个问题?
  • 今天我很惊讶地发现这些方法在我的应用程序中被自动调用。我总是在活动中注册他们。您还可以找到它们的事件:FederatedAuthentication.FederationConfigurationCreated +=FederatedAuthentication_FederationConfigurationCreated;

标签: asp.net wif


【解决方案1】:

您的 web.config 中缺少以下行:

在 configSections 元素中:

<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

在 system.webServer 元素中

 <modules>
  <remove name="FormsAuthentication" />
  <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
  <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
</modules>

您的观众 Uris 为空。您必须指定您的 Web 应用程序,以便它可以使用此功能。所以,添加这一行:

  <audienceUris>
    <add value="http://localhost/dss.web.frontend"/>
  </audienceUris>

如果在此更改后您的问题再次出现,您可以实现从 WSFederationAuthenticationModule 派生的自定义身份验证模块。像这样:

public class CustomAuthenticationModule : WSFederationAuthenticationModule
{
    public CustomAuthenticationModule()
    {
        base.SecurityTokenReceived += CustomAuthenticationModule_SecurityTokenReceived;
    }

    public void CustomAuthenticationModule_SecurityTokenReceived(object sender, SecurityTokenReceivedEventArgs e)
    {

    }

    protected override void OnAuthenticateRequest(object sender, EventArgs args)
    {
        base.OnAuthenticateRequest(sender, args);
    }
}

然后只是在配置更改而不是 WSFederationAuthenticationModule 中将 CustomAuthenticationModule 放入适当的命名空间和程序集签名。所以你可以在你的委托中拦截调用。

希望这对你有帮助。

拉斯特科

【讨论】:

    【解决方案2】:

    将以下内容添加到您的 Global.asax.cs:

    void Application_Start()
    {
        FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
    }
    
    
    void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
    {
        FederatedAuthentication.WSFederationAuthenticationModule.RedirectingToIdentityProvider += WSFederationAuthenticationModule_RedirectingToIdentityProvider;
    } 
    

    感谢https://stackoverflow.com/a/9207505/13932

    【讨论】:

      【解决方案3】:

      确保您从新命名空间System.IdentityModel.Services 引用WSFederationAuthenticationModule

      在将解决方案迁移到 .NET 4.5 后,我仍然从旧的 Microsoft.IdentityModel.Web 命名空间引用它。

      找到我的答案here

      【讨论】:

        【解决方案4】:

        听起来您的配置中可能缺少WSFederationAuthenticationModule。确保你有这个在system.webServer\modules:

        <add name="WSFederationAuthenticationModule" type="Microsoft.IdentityModel.Web.WSFederationAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" preCondition="managedHandler" />
        

        这个在system.web\httpModules:

        <add name="WSFederationAuthenticationModule" type="Microsoft.IdentityModel.Web.WSFederationAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        

        阅读here了解更多信息。

        【讨论】:

          【解决方案5】:

          要检查的一点是,您在 web.config 模块和 Global.asax.cs using 语句之间引用了一致的程序集。由于RedirectingToIdentityProviderEventArgs 类型存在于System.IdentityModel.ServicesMicrosoft.IdentityModel.Web 中(从.NET 4.5 开始),您可能会从web.config 中的一个程序集添加模块,但从Global.asax 中的另一个程序集引用事件arg。 CS。我认为那会失败。

          【讨论】:

            【解决方案6】:

            我的问题是我在 system.web/httpModules 和 system.webServer/modules 部分都添加了以下模块。

              <add name="WsFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
              <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
            

            从 system.web/httpModules 部分中删除元素解决了这个问题,并且所有附加到 WSFederationAuthenticationModule 实例的事件都被触发了。

            【讨论】:

              【解决方案7】:

              对于子类WSFederationAuthenticationModule 并因此更改web.config 中的模块注册名称并使用自动接线方法(在global.asax.cs 内)的人,您还需要更改开头方法名。

              例如,如果您在system.webServer\modules 中有以下内容

              <add name="CustomWsFedModule" type="SomeLib.CustomWSFederationAuthenticationModule" preCondition="managedHandler" />
              

              您将需要在您的global.asax.cs 中提供以下内容

              public class MvcApplication : System.Web.HttpApplication
              { 
                  void CustomWsFedModule_RedirectingToIdentityProvider(object sender, RedirectingToIdentityProviderEventArgs e)
                  {
                      //some code
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 2021-02-20
                • 1970-01-01
                • 1970-01-01
                • 2015-09-08
                • 2014-03-06
                • 2015-11-14
                • 2021-04-24
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多