我如何将federationConfiguration从Web.config中移出并移到某些自定义配置文件中,并通过代码动态加载它 [英] How do I move federationConfiguration out of web.config and to some custom config file and load it dynamically by code

查看:255
本文介绍了我如何将federationConfiguration从Web.config中移出并移到某些自定义配置文件中,并通过代码动态加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.config中配置了配置,并且运行正常。

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

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

如何将其从web.config中移至自定义配置文件并从代码中加载?

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.

推荐答案

您可以从global.asax进入WIF事件

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

如果您对赋予哪个对象有什么疑问,可以始终暂时切换回配置并通过同一事件检查运行时值。不要低估WIF提供的配置的复杂性和丰富性。

通常,您想让代码配置和 web.config配置混合使用。 config仍用于配置config的某些可变部分,并且代码用于更固定的部分...

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...

这篇关于我如何将federationConfiguration从Web.config中移出并移到某些自定义配置文件中,并通过代码动态加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆