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

标签 c# wcf wif sts-securitytokenservice

我正在使用 .NET 4.5 中包含的 WIF 框架创建 STS。我正在使用 WSTrustServiceHost 自托管此 STS(目前)类(class)。为此,我正在执行以下操作:

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();
// ...

如您所见,我正在传入 trueloadConfig SecurityTokenServiceConfiguration 的参数构造函数,其中,文档说:

true to load settings from the configuration file; otherwise false.

我有一个 identityConfiguration元素在我的配置文件中,但它似乎没有被加载。我可以更改配置文件,例如我可以更改 securityTokenHandlers ,并且这些更改未反射(reflect)在构造的 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/>安全 token 处理程序,并且在收到请求时仍会使用 token 处理程序。

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

最佳答案

经过大量探索,我发现 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);

关于c# - 如何使 SecurityTokenServiceConfiguration 从 app.config 加载配置信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14528102/

相关文章:

wif - 在Docker中启用Windows Identity Foundation

c# - 约束引用 'string'无法解析为类型。 (netcoreapp3.0)

c# - 使用 HttpClient 获取

c# - 为什么列表后需要分号

c# - 为什么向我的 WCF 服务操作添加参数会起作用?

sharepoint - 在不将版权声明映射到AD帐户的情况下,如何执行WIF/版权声明模拟?

javascript - 获取外键表数据

.net - 在 SQL2005 中部署 WCF 客户端程序集

c# - 在内存中存储数组的最佳实践

asp.net-mvc-3 - 将 WIF 与 asp.net MVC 3 一起使用,我在哪里定义 STS 注销端点?