gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:09:12 24 4
gpt4 key购买 nike

我已使用最新 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com