【问题标题】:Using Windsor Castle and WcfFacility to create WCF proxies with Message Security and username credentials使用 Windsor Castle 和 WcfFacility 创建具有 Message Security 和用户名凭据的 WCF 代理
【发布时间】:2011-06-27 12:03:04
【问题描述】:

好的,我们正在使用 message security用户名凭据(以及 X509 证书加密)与我们的 WCF 服务进行通信。我对这种方法不满意,但这不是问题的重点,我不想深入探讨。

我正在使用 Windsor CastleASP NET Web 窗体 + MVC 混合 中生成代理。我们正在使用表单身份验证并使用用户凭据与 WCF 服务进行通信——这将有助于审核所有调用。正如我所说,我对这种方法不满意,但这不是重点。

我创建了CustomCredentials 类,它继承了AbstractCredentials 类,WcfFacility 很高兴地使用它来配置我的代理。正如您将在下面看到的,我的所有设置都只是几行。我在下面创建了单元测试,它演示了我所做的一切:创建代理,进行调用,然后在循环中释放它。现在我期待这个测试能够工作,但它没有,我得到了

Expected: 10 But was: 1

我没有包含绑定,但这无关紧要,因为我说过我正在使用带有 X509 证书的 Message Security。

我知道对于具有消息安全性的通道工厂,一旦打开就无法更改凭据。这是同一个问题吗?

这是 WcfFacility 中的错误还是限制?

这里是代码

[TestFixture]
public class Harness
{

    private IWindsorContainer _container;
    public static int NumberOfTimesCredentialsConfigured = 0;

    [SetUp]
    public void Setup()
    {
        _container = new WindsorContainer().AddFacility<WcfFacility>();

    Component
        .For<IFrameworkUsers>()
        .ActAs(DefaultClientModel
        .On(WcfEndpoint.FromConfiguration("FrameworkUsersService"))
        .Credentials(new CustomCredentials()))
        .LifeStyle.Transient);
    }

    [Test]
    public void MultipleProxyTest()
    {

        const int Runs = 10;
        NumberOfTimesCredentialsConfigured = 0;
        for (int i = 0; i < Runs; i++)
        {
            IFrameworkUsers frameworkUsers = _container.Resolve<IFrameworkUsers>();
            frameworkUsers.CreateUserSession();
            _container.Release(frameworkUsers);
        }

        Assert.AreEqual(Runs, NumberOfTimesCredentialsConfigured);
                    // FAILS!!! Expected: 10  But was:  1
    }

    [TearDown]
    public void TearDown()
    {

    }


}

public class CustomCredentials : AbstractCredentials
{


    #region Overrides of AbstractCredentials

    protected override void ConfigureCredentials(ClientCredentials credentials)
    {
        credentials.UserName.UserName = "testuser";
        credentials.UserName.Password = "abcdef";

        Harness.NumberOfTimesCredentialsConfigured++;
    }

    #endregion
} 

【问题讨论】:

  • 你有没有找到解决这个问题的方法?我自己也遇到了同样的问题。
  • @DamianPowell no :( 我在城堡论坛上发帖但没有回复。这是 WCF 设施中的设计问题,他们在其中缓存服务通道,这在没有安全性的情况下是可以的,但在安全性下不起作用。

标签: c# wcf castle-windsor ioc-container wcffacility


【解决方案1】:

您的凭据和IWcfEndpoint 通常(这是DefaultClientModel.On(...) 调用的结果)仅在配置容器时创建一次。如果您想每次都提供不同的凭据 - 您需要使它们成为动态依赖项,如下所示:

_container.Register(Component.For<IFrameworkUsers>()
    .AsWcfClient()
    .DependsOn(
        (k, d) => d["endpoint"] = 
            new DefaultClientModel(WcfEndpoint.FromConfiguration("FrameworkUsersService"))
                .Credentials(new CustomCredentials())
     )
    .LifeStyle.Transient);

String endpoint 这里是 WcfFacility 使用的依赖项的名称。我不确定确切的位置,但它已在某些拦截器中解决(您可以在 lambda 中设置断点并调试您的测试以查看调用堆栈)。我假设它与 AsWcfClient(IWcfEndpoint endpoint) 方法参数的名称相匹配。

所以答案是否定的,它既不是错误也不是 WcfFacility 的限制。

【讨论】:

    【解决方案2】:

    我在城堡论坛发帖,没有回复。这是 WCF 设施中设计的一个问题,他们缓存服务通道,这在没有安全性的情况下是可以的,但在安全性的情况下不起作用。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多