开发者

add custom header for security authentication in wcf

开发者 https://www.devze.com 2023-04-11 22:07 出处:网络
i am using webHttpBinding to expose the wcf serivces and return JSON format, i want to make some security authentication in header like:

i am using webHttpBinding to expose the wcf serivces and return JSON format, i want to make some security authentication in header like:

CustomerServiceClient client = new CustomerServiceClient();

            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                MessageHeader header = MessageHeader.CreateHeader("username", "http://tempuri.org", "testuser");

                OperationContext.Current.OutgoingMessageHeaders.A开发者_C百科dd(header);

            }

but i got this error message: Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers.

i google long time but no result for me.

thanks.


I believe the MessageHeader is intended for SOAP headers and SOAP services as per the first line of the MSDN documentation for the MessageHeader class:

http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messageheader.aspx

Since you are using webHttpBinding I guess you are creating a REST service not a SOAP service. In this case you should use the System.Net.WebClient class:

http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

or similar and set the header using its Headers property. To be clear, this will set HTTP headers, not SOAP message headers, since you are not using SOAP.

If you really want to set a username (and I guess a password) as in your sample code, then you can use the Credentials property of the WebClient rather than set the authorization header using the Headers property.

0

精彩评论

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

关注公众号