如何使SecurityTokenServiceConfiguration从app.config加载配置信息?

时间:2013-01-25 18:28:34

标签: c# wcf wif sts-securitytokenservice

我正在使用.NET 4.5中包含的WIF框架创建一个STS。我使用WSTrustServiceHost类自我托管这个STS(现在)。为此,我正在做以下事情:

var conf = new SecurityTokenServiceConfiguration("isser name here", true)
{
    DisableWsdl          = true,
    SecurityTokenService = typeof(MyTokenService),
};
var ct   = new WSTrustServiceContract(conf);
var host = new WSTrustServiceHost(ct);

host.Open();
// ...

正如您所看到的,我已将true传递给loadConfig构造函数的SecurityTokenServiceConfiguration参数,该文档说明了这些参数:

  

true 从配置文件加载设置;否则错误

我的配置文件中有一个identityConfiguration元素,但似乎没有加载。我可以对配置文件进行更改,例如。我可以更改securityTokenHandlers,这些更改不会反映在构建的SecurityTokenServiceConfiguration

在我的app.config文件中,我有以下内容:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="sts_behavior">
                <serviceCredentials useIdentityConfiguration="true" identityConfiguration="the_issuer_id">
                    <serviceCertificate findValue="7A5D7EB05EC741E45BF4EDA7E574F58DC31EF290" x509FindType="FindByThumbprint" storeName="My" storeLocation="LocalMachine" />
                </serviceCredentials>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <ws2007HttpBinding>
            <binding name="sts_binding">
                <security mode="Message">
                    <message clientCredentialType="UserName" />
                </security>
            </binding>
        </ws2007HttpBinding>
    </bindings>
    <services>
        <service name="System.ServiceModel.Security.WSTrustServiceContract" behaviorConfiguration="sts_behavior">
            <endpoint address="http://my-machine:54512/tokens" binding="ws2007HttpBinding" contract="System.ServiceModel.Security.IWSTrust13SyncContract" bindingConfiguration="sts_binding" />
        </service>
    </services>
</system.serviceModel>

可以看出,<serviceCredentials>元素是指配置文件中存在的<identityConfiguration>元素,如果我将此名称更改为与<identityConfiguration>元素不匹配,则错误是打开服务主机时抛出。但是,仍然没有使用此<identityConfiguration>元素,因为我可以<clear/>安全令牌处理程序,并且在请求进入时仍然使用令牌处理程序。

如何使用最少的编程配置配置和自托管自定义STS?

1 个答案:

答案 0 :(得分:2)

经过深入探索,我发现SecurityTokenServiceConfiguration构造函数的一个重载允许指定加载配置的<identityConfiguration>元素的名称:

//
// Summary:
//     Initializes a new instance of the System.IdentityModel.Configuration.SecurityTokenServiceConfiguration
//     class that has the specified issuer name and signing credentials. Settings
//     are loaded from the specified named configuration.
//
// Parameters:
//   issuerName:
//     The issuer name. Sets the System.IdentityModel.Configuration.SecurityTokenServiceConfiguration.TokenIssuerName
//     property.
//
//   signingCredentials:
//     The signing credentials for the STS. Sets the System.IdentityModel.Configuration.SecurityTokenServiceConfiguration.SigningCredentials
//     property.
//
//   serviceName:
//     The name of the <identityConfiguration> element from which the configuration
//     is to be loaded.
public SecurityTokenServiceConfiguration(string issuerName, SigningCredentials signingCredentials, string serviceName);