【问题标题】:add custom header for security authentication in wcf在 wcf 中添加用于安全身份验证的自定义标头
【发布时间】:2011-12-03 18:56:55
【问题描述】:

我正在使用 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)”不支持添加邮件标头。

我google了很长时间,但没有结果。

谢谢。

【问题讨论】:

    标签: wcf json soap soapheader


    【解决方案1】:

    根据 MessageHeader 类的 MSDN 文档的第一行,我相信 MessageHeader 旨在用于 SOAP 标头和 SOAP 服务:

    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 属性设置授权标头。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2019-02-21
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多