使用 WCF 将自定义标头添加到安全元素中

发布于 2024-09-10 16:37:25 字数 595 浏览 4 评论 0原文

我可以在信封/标头/安全元素中添加并读取自定义标头吗?我尝试使用 MessageHeader 属性,但这不允许我将标头放入 Security 元素中。

我创建了一个实现 IClientMessageInspector 的类,认为我可以像这样访问 Security 标头:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
   MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
   request = buffer.CreateMessage();

   Message originalMessage = buffer.CreateMessage();
   foreach (MessageHeader h in originalMessage.Headers)
   {
      Console.WriteLine("\n{0}\n", h);
   }

   return null;
}

但是 Security 标头不存在于originalMessage.Headers 对象中。

Can I add and read a custom header in the Envelope/Header/Security element? I tried using the MessageHeader attribute, but that does not allow me to put the header in the Security element.

I created a class that implements IClientMessageInspector thinking that I could access the Security header like so:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
   MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
   request = buffer.CreateMessage();

   Message originalMessage = buffer.CreateMessage();
   foreach (MessageHeader h in originalMessage.Headers)
   {
      Console.WriteLine("\n{0}\n", h);
   }

   return null;
}

But the Security header is not present in the originalMessage.Headers object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-09-17 16:37:25

创建自定义消息编码器: http://msdn.microsoft.com/en-us /library/ms751486.aspx

您可以在编码器的 WriteMessage 覆盖中访问消息标头。请注意,消息的标头属性将不包含安全标头(尽管这可能取决于您使用的安全类型)。使用 Message.WriteMessage(XmlWriter) 将消息写入流或文件。流/文件将包含通过线路发送之前的消息内容,包括安全元素。从那里,您可以根据需要修改消息并返回包含您的更改的 ArraySegment。

Create a custom message encoder: http://msdn.microsoft.com/en-us/library/ms751486.aspx.

You can access the message headers in your encoder's WriteMessage override. Note that the Message's Headers property will not contain the Security header (though this may depend on the type of security you're using). Write out the message to a stream or file using, say, Message.WriteMessage(XmlWriter). The stream/file will contain the contents of the message just before being sent over the wire, including the Security element. From there, you can modify your message as necessary and return an ArraySegment including your changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文