3

我已经使用最新 WIF SDK(用于 .NET 4.0)附带的被动和主动案例的项目模板成功地创建了一个有效的自定义 STS。一切都按预期工作。

我现在正在尝试将我的 Web 应用程序和服务升级到 .NET 4.5,包括我的自定义 STS。我已经能够将 Microsoft.IdentityModel.xxx 中的所有命名空间/类映射到框架中内置的新命名空间/类,但有 1 个例外 - WSTrustServiceHostFactory。

该类似乎不再存在,我无法弄清楚如何替换它提供的功能。即,从此链接:http: //msdn.microsoft.com/en-us/library/microsoft.identitymodel.protocols.wstrust.wstrustservicehostfactory

<%@ServiceHostLanguage="C#"Debug="true"Service="XXX.XXX.MyActiveSTSConfiguration"Factory="Microsoft.IdentityModel.Protocols.WSTrust.WSTrustServiceHostFactory"%>

我的被​​动 STS 升级到 4.5 后目前正在工作。我需要找到使用 4.5 框架实例化活动 SecurityTokenService 的适当/推荐方法(理想情况下,使用 web.config 作为 SDK 项目模板中的大多数配置)。任何建议表示赞赏。

4

1 回答 1

8

一旦我弄清楚了一些事情,这变得非常简单。

服务主机标记:

<%@ ServiceHost Language="C#" Debug="true" Service="XXX.XXX.MyActiveSTSConfiguration" Factory="XXX.XXX.CustomWSTrustServiceHostFactory" %>

这是我的自定义工厂类实现。关键是,由于您不能再使用 WSTrustServiceFactory 为您创建 WSTrustServiceHost,因此您必须在 CreateServiceHost 方法覆盖中自己显式创建一个。

public class CustomWSTrustServiceHostFactory 
    : ServiceHostFactory {

    /// <summary>
    /// Initializes a new instance of the <see cref="CustomWSTrustServiceHostFactory"/> class.
    /// </summary>
    public CustomWSTrustServiceHostFactory() 
        : base() { }

    /// <summary>
    /// Creates and configures a <see cref="WSTrustServiceHost"/> with a specific base address.
    /// </summary>
    /// <param name="serviceType">Specifies the type of service to host (ignored).</param>
    /// <param name="baseAddresses">The <see cref="T:Uri"/> array that contains the base addresses for the service.</param>
    /// <returns>A <see cref="WSTrustServiceHost"/> with a specific base address.</returns>
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) {
        var config = new PortalActiveSTSConfiguration();
        var host = new WSTrustServiceHost(config, baseAddresses);
        //var host = base.CreateServiceHost(serviceType, baseAddresses);
        var serviceBehavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
        serviceBehavior.AddressFilterMode = AddressFilterMode.Any;
        return host;
    }

    /// <summary>
    /// Creates and configures a <see cref="WSTrustServiceHost"/> with a specific base address.
    /// </summary>
    /// <param name="constructorString">The constructor string (ignored).</param>
    /// <param name="baseAddresses">The <see cref="T:Uri"/> array that contains the base addresses for the service.</param>
    /// <returns></returns>
    public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) {
        var config = new PortalActiveSTSConfiguration();
        var host = new WSTrustServiceHost(config, baseAddresses);
        //var host = base.CreateServiceHost(constructorString, baseAddresses);
        var serviceBehavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
        serviceBehavior.AddressFilterMode = AddressFilterMode.Any;
        return host;
    }

}

自定义服务配置类实现(基本上是 WIF 4.0 STS 模板附带的):

public class MyActiveSTSConfiguration 
    : SecurityTokenServiceConfiguration {

    public MyActiveSTSConfiguration()
        : base(
            WebConfigurationManager.AppSettings[ISSUER_NAME],
                new X509SigningCredentials(
                    CertificateUtil.GetCertificate(
                        StoreName.My, StoreLocation.LocalMachine, X509FindType.FindByThumbprint,
                        WebConfigurationManager.AppSettings[SIGNING_CERTIFICATE_THUMBPRINT], 
                        true)
                )
        ) {
        this.SecurityTokenService = typeof(MyActiveSTS);
    }

}
于 2012-09-05T15:22:26.487 回答