How do I move federationConfiguration out of web.config and to some custom config file and load it dynamically by code

时间秒杀一切 提交于 2019-12-09 06:54:31

问题


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.


回答1:


You can tap into the WIF event from your global.asax

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

}

In that handler you can adapt the configuration at runtime. Here is some code to give you an idea. The end code will be more complex.

   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;
    }

If you have any doubts on what value to give to which object, you can always temporarily switch back to the configuration and inspect the runtime values through the same event. Don't underestimated the complexity and richness of the configuration delivered by WIF out of the box.
In general, you migth want a mixture of "code config" and "web.config config" were the web.config is still used to configure certain more variable parts of the config and the code is used for the more unchangable pieces...



来源:https://stackoverflow.com/questions/24316833/how-do-i-move-federationconfiguration-out-of-web-config-and-to-some-custom-confi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!