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

标签 wif .net-4.5

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

我现在正在尝试将我的 Web 应用程序和服务升级到 .NET 4.5,包括我的自定义 STS。我已经能够将 Microsoft.IdentityModel.xxx 中的所有命名空间/类映射到框架中内置的新命名空间/类,但有 1 个异常(exception) - 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 的适当/推荐方法(理想情况下,使用 web.config 作为 SDK 项目模板中的大多数配置)。任何建议表示赞赏。

最佳答案

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

服务主机标记:

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

}

关于wif - 尝试将 WIF 4.0 自定义 STS(来自项目模板)转换为使用 .NET 4.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204104/

相关文章:

c# - 创建用于验证的属性并更改参数值

wif - 如何将(声明)安全 token 传递给 WIF 启用 WCF 服务

azure - Azure ACS 的自定义登录页面不起作用

.net - 是否可以对复杂的声明进行建模(分层/嵌套/等)?

c# - Razor 语法在编译前在 View 中给出错误(无智能感知)

.net - 在哪里可以下载适用于我的构建服务器的 .NET 4.5 多目标包?

.net - WCF 身份验证用户 Google/Facebook 等

asp.net - 如何使安全 token 在被动 STS 设置中自动过期?

C# 静态方法内联

c# - 使用 "very very"大数组