Bassist
Bassist

Reputation: 181

Pass message headers on WCF via netTcpBinding

After banging my head for a day I'm asking for help. I have a Web Forms application with WCF service hosting at the same machine. The WCF service has methods with some sort of custom permissions (for instance, only "admin" can set passwords for users).

Web forms app uses Forms Authentication so I have HttpContext.Current.User with all the roles this uses has. So I used an AuthCookie to pass the object via HttpRequestMessageProperty and read it like this:

 HttpRequestMessageProperty property =
OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;    

With basicHttpBinding that worked fine. But then I needed to implement net.tcp protocol so the HttpRequestMessageProperty doesn't work anymore. I've tried to add it by MessageHeader but that doesn't work either.

OperationContext.Current.IncomingMessagePropertieshas got none of my headers...

So how can I pass the user roles list to my WCF? I am using Windows security like this but I need to pass roles from HttpContext.Current.User:

 <netTcpBinding>
    <binding transactionFlow="True">
      <security mode="Transport">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </netTcpBinding> 

Upvotes: 2

Views: 1824

Answers (1)

Bassist
Bassist

Reputation: 181

OK so I found the working way for transferring the role info. Although this is still unclear why standard ways didn't work for me here is the right one (thanks to Guy Burstein):

Client:

                MessageHeader<string> header = new MessageHeader<string>(roleData);
                MessageHeader untypedHeader = header.GetUntypedHeader(ProjectParam.Role, "justASampleNamespace");
                OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader);

WCF:

string roles = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(ProjectParam.Role, "justASampleNamespace");
if(roles.Contains(ProjectParam.AdminLoginID))
{
//Now business logic comes
}

ProjectParam.Role is just an internal constant utilized for key and the second parameter is a sample string (MSDN says the "namespace")

Upvotes: 2

Related Questions