开发者

Adding authentication to security header in WCF to consume Metro WSIT service

I use this simple way to attach username and password to the SOAP request header. This works fine inside Java boundaries, but I want to be able to call it with my WCF client. How do I do this?

I've tried the following code, but i开发者_运维知识库t does not include the credentials in the header:

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

Thanks in advance!


That is quite awful non-standardized way. It uses custom HTTP Headers so you cannot expect that built in WCF mechanism will magically support such approach. How should WCF know that you want to add custom non-standard HTTP header to HTTP request (not SOAP header)?

Use this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜