PeteJ
PeteJ

Reputation: 121

Trying to convert WIF 4.0 custom STS (from project template) to use .NET 4.5

I have successfully created a working custom STS using the project templates for the passive and active cases that came with the most recent WIF SDK (for .NET 4.0). Everything works as desired.

I am now trying to upgrade my web applications and services to .NET 4.5, including my custom STS. I have been able to map all of the namespaces/classes from Microsoft.IdentityModel.xxx to the new namespaces/classes built into the framework with 1 exception - WSTrustServiceHostFactory.

That class no longer seems to exist, and I cannot figure out how to replace the functionality it provided. Namely, from this link: 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"%>

My passive STS is currently working after upgrading to 4.5. I need to find an appropriate/recommended method of instantiating an active SecurityTokenService using the 4.5 framework (ideally, using web.config for most configuration as in the SDK project templates). Any suggestions are appreciated.

Upvotes: 3

Views: 2029

Answers (1)

PeteJ
PeteJ

Reputation: 121

This turned out to be pretty simple once I figured a few things out.

Service host markup:

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

Here's my custom factory class implementation. The key is that since you can no longer use WSTrustServiceFactory to create a WSTrustServiceHost for you, you have to explicitly create one yourself in the CreateServiceHost method overrides.

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

}

Custom service configuration class implementation (basically what came with the WIF 4.0 STS template):

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

}

Upvotes: 8

Related Questions