3

I have my configuration in web.config and it works fine.

  <configuration>
  <system.identityModel.services>
    <federationConfiguration>
....
 </federationConfiguration>
  </system.identityModel.services>
</configuration>

How do I move this out of web.config to a custom config file and load it from code?

I want to use the same structure of this configuration so that I do not have to change anything in code if I have to change this configuration file.

4

1 回答 1

6

您可以从 global.asax 访问 WIF 活动

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    FederatedAuthentication.FederationConfigurationCreated += FederatedAuthenticationOnFederationConfigurationCreated;

}

在该处理程序中,您可以在运行时调整配置。这里有一些代码可以给你一个想法。结束代码会更复杂。

   private void FederatedAuthenticationOnFederationConfigurationCreated(object sender, FederationConfigurationCreatedEventArgs args)
    {
        var identityConfiguration = new IdentityConfiguration(loadConfig:false);
        identityConfiguration.SecurityTokenHandlers.Clear();
        //...
        identityConfiguration.SecurityTokenHandlers.Add(new Saml2SecurityTokenHandler());
        //...
        var configuration = new FederationConfiguration(loadConfig: false)
        {
            CookieHandler = new ChunkedCookieHandler(),
            //...
            IdentityConfiguration = identityConfiguration
        };
        args.FederationConfiguration = configuration;
    }

如果您对赋予哪个对象的值有任何疑问,您始终可以暂时切换回配置并通过同一事件检查运行时值。不要低估 WIF 开箱即用提供的配置的复杂性和丰富性。
一般来说,你可能想要“代码配置”和“web.config 配置”的混合,因为 web.config 仍然用于配置配置的某些更多可变部分,而代码用于更不可更改的部分......

于 2014-06-20T03:09:39.990 回答