在WCF中向安全头添加身份验证以使用Metro WSIT服务

时间:2011-10-03 22:08:57

标签: wcf web-services soap wsit

我使用this简单的方法将用户名和密码附加到SOAP请求标头。这在Java边界内工作正常,但我希望能够使用我的WCF客户端调用它。我该怎么做?

我尝试过以下代码,但它不包含标题中的凭据:

wsClient.ClientCredentials.UserName.UserName = "Hello";
wsClient.ClientCredentials.UserName.Password = "World";

提前致谢!

1 个答案:

答案 0 :(得分:0)

这是非常糟糕的非标准化方式。它使用自定义HTTP标头,因此您不能指望内置的WCF机制会神奇地支持这种方法。 WCF应该如何知道您要将自定义非标准HTTP标头添加到HTTP请求(而不是SOAP标头)?

使用此:

var proxy = new YourServiceClient();
using (var scope = new OperationContextScope(proxy.InnerChannel))
{
    var prop = new HttpRequestMessageProperty();
    prop.Headers.Add("UserName", "Hello");
    prop.Headers.Add("Password", "World");

    OperationContext context = OperationContext.Current;
    context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = prop;

    proxy.CallYourOperation();
}
相关问题