我正在使用webHttpBinding来公开wcf服务并返回JSON格式,我想在标题中进行一些安全认证,如:
CustomerServiceClient client = new CustomerServiceClient();
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
MessageHeader header = MessageHeader.CreateHeader("username", "http://tempuri.org", "testuser");
OperationContext.Current.OutgoingMessageHeaders.Add(header);
}
但我收到此错误消息: 信封版本'EnvelopeNone(http://schemas.microsoft.com/ws/2005/05/envelope/none)'不支持添加邮件标题。
我谷歌很长时间,但对我没有结果。
感谢。
答案 0 :(得分:1)
我认为MessageHeader适用于SOAP标头和SOAP服务,根据MessageHeader类的MSDN文档的第一行:
http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messageheader.aspx
由于您正在使用webHttpBinding,我猜您正在创建REST服务而不是SOAP服务。在这种情况下,您应该使用System.Net.WebClient类:
http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
或类似的,并使用其Headers属性设置标头。要明确的是,这将设置HTTP标头,而不是SOAP消息标头,因为您没有使用SOAP。
如果您真的想要在示例代码中设置用户名(我猜密码),那么您可以使用WebClient的Credentials属性,而不是使用Headers属性设置授权标头。