Click here to Skip to main content
14,447,414 members

WCF passing a token from client to server using the client and server behavior

tiran_kaskas asked:

Open original thread
I have done the following steps:

1. Client interacts with an STS, which will issue the token.
2. The client uses some other services.

The client needs to present the token to any service it interacts with.
For this reason I used the client and service behavior.
Every time the client calls the service, I put the SamlSecurityToken into the message being sent (using the behavior):
public object BeforeSendRequest(ref Message request, IClientChannel channel)
            UserInfo userInfo = new UserInfo {Username="aaa", password="1234"};
            TokenEngine te = new TokenEngine();

            SamlSecurityToken Token = (SamlSecurityToken)te.CreateToken(userInfo, null);

            //Add the token to the message header
            request.Headers.Add(MessageHeader.CreateHeader("SecurityToken", "", Token));
            return null;

On the service side, I am trying to extract the token from the message:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            //Extract the security token from the received message
            SamlSecurityToken callerToken = request.Headers.GetHeader<samlsecuritytoken>("SecurityToken", "");

            return null;

The problem comes on the service side: I get a null reference exception when trying to get the token.

Am I doing something wrong?
Any alternatives?
Tags: .NET (.NET3.5), WCF


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100