尝试将WIF 4.0自定义STS(从项目模板)转换为使用.NET 4.5

时间:2012-08-30 19:30:20

标签: wif .net-4.5

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

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

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

1 个答案:

答案 0 :(得分: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);
    }

}