简体   繁体   English

如何将安全令牌添加到对WCF服务发出的请求的标头中?

[英]How to add a security token to the header of a request made to WCF service?

I have implemented a wcf service and will be using TCP for transport. 我已经实现了一个wcf服务,并将使用TCP进行传输。 I wanted to know, how do I stick the security token (guid in our case) to the header of a request made to wcf service? 我想知道,如何将安全令牌(在我们的情况下为guid)粘贴到对wcf服务的请求的标头中? Can anyone help me in this or give me any ideas how to do this? 任何人都可以帮助我或给我任何想法如何做到这一点? Or if there was any specific concept that I should learn? 或者,如果有任何具体的概念我应该学习?

The operation context offers collections of incoming and outgoing headers, available via the IncomingMessageHeaders and OutgoingMessageHeaders properties: 操作上下文提供传入和传出标头的集合,可通过IncomingMessageHeaders和OutgoingMessageHeaders属性获得:

public sealed class OperationContext : ...
{
   public MessageHeaders IncomingMessageHeaders {get;}
   public MessageHeaders OutgoingMessageHeaders {get;}
   //More members
}

Each collection is of the type MessageHeaders (that is, a collection of MessageHeader objects): 每个集合都是MessageHeaders类型(即MessageHeader对象的集合):

public sealed class MessageHeaders : ...
{
   public void Add(MessageHeader header);
   public T GetHeader<T>(int index);
   public T GetHeader<T>(string name,string ns);
   //More members
}

public abstract class MessageHeader
{...}

public class MessageHeader<T>
{
   public MessageHeader();
   public MessageHeader(T content);
   public T Content {get;set;}
   public MessageHeader GetUntypedHeader(string name,string ns);
   //More members
}

You can use that to pass the Guid into the message header. 您可以使用它将Guid传递到邮件头中。

//Client code:
MessageHeader<Guid> tokenHeader = new MessageHeader<Guid>(someGuid);
MyContractClient proxy = new MyContractClient();
using(OperationContextScope contextScope =
                  new OperationContextScope(proxy.InnerChannel))
{
   OperationContext.Current.OutgoingMessageHeaders.Add(
                  tokenHeader .GetUntypedHeader("Guid","System"));
   proxy.MyMethod();
}
proxy.Close();

Take a look at this link here MSDN it shows you how to format a Security Header Formatting Security Headers | 看看这个链接在这里MSDN它向您展示如何格式化安全头格式化安全头| Security Protocols this MSDN Site will explain what types of Protocols you can use and Header Content. 安全协议本MSDN站点将解释您可以使用哪些类型的协议和标头内容。

This is what Microsofts Security Header looks like passing a GUID 这就是微软安全标题看起来像传递GUID

<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0">
  </u:Timestamp><o:UsernameToken u:Id="uuid-b96fbb3a-e646-4403-9473-2e5ffc733ff8-1">
</o:UsernameToken></o:Security>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM