user1086046
user1086046

Reputation: 55

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. 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? 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?

Upvotes: 1

Views: 7972

Answers (3)

Kassem
Kassem

Reputation: 8276

The operation context offers collections of incoming and outgoing headers, available via the IncomingMessageHeaders and OutgoingMessageHeaders properties:

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):

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.

//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();

Upvotes: 4

MethodMan
MethodMan

Reputation: 18863

Take a look at this link here MSDN it shows you how to format a Security Header Formatting Security Headers | Security Protocols this MSDN Site will explain what types of Protocols you can use and Header Content.

This is what Microsofts Security Header looks like passing a 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>

Upvotes: 0

NoWar
NoWar

Reputation: 37632

Hope this will help you.

How to: Enable the WCF Authentication Service http://msdn.microsoft.com/en-us/library/bb398990.aspx.

and there is other one http://blog.adnanmasood.com/2010/04/29/step-by-step-guide-for-authenticating-wcf-service-with-username-and-password-over-ssl/

Upvotes: 0

Related Questions