0

We have a WCF service in .NET 4.7.2. This WCF has been configured for "oAuth0"(Not Aure but a third party provider). We are able to call a method on this service using PostMan successfully. What we did is we generated a token on side and pass this token while hitting end point using postman. This way we have ensured our configuration for oAuth0 is working fine.

Now we are trying to call the endpoint in WCF, using a console client but its not working for us. We have generated a proxy class and using the same to call end point on WCF.

// Following is code sample I have pickeup from internet only.

using (Service1Client client = new Service1Client())
        {
            token = "Bearer " + token; // assume we have value for token here
            using (new System.ServiceModel.OperationContextScope(client.InnerChannel))
            {
                var head = System.ServiceModel.Channels.MessageHeader.CreateHeader("Authorization", "http://localhost:53515/Service1.svc", token);
                OperationContext.Current.OutgoingMessageHeaders.Add(head);
            }

            string response = client.GetDataWoToken(12345);
            if (response != null)
            {
                System.Console.WriteLine("respone from WCF Service was ::->>  " + response);
            }

            System.Console.ReadLine();

        }

And this is how I am tryin to access the header in WCF Service

string authorizationHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];

On debugging, its clear that "MessageHeader" is not the right option.

Any pointers would be useful.

Bests regards & thanks in Adance

S

spalMcc
  • 21
  • 4

3 Answers3

0

can you just try following code? I am not sure why are using "using" keyword.

using (Service1Client client = new Service1Client())
{
token = "Bearer " + token; // assume we have value for token here

// Create a new MessageHeader object and set its properties
var head = new System.ServiceModel.Channels.MessageHeader();
head.Name = "Authorization";
head.Namespace = "http://localhost:53515/Service1.svc";
head.Value = token;

// Add the MessageHeader object to the OperationContext
OperationContext.Current.OutgoingMessageHeaders.Add(head);

// Call the GetDataWoToken method on the client
string response = client.GetDataWoToken(12345);

// Check if the response is not null
if (response != null)
{
    // Print the response to the console
    System.Console.WriteLine("Response from WCF Service was ::->> " + response);
}

// Read a line from the console
System.Console.ReadLine();

}

0

You can assign header to your client like this:

  IContextChannel contextChannel = (IContextChannel)myServiceProxy;
  using (OperationContextScope scope = new OperationContextScope(contextChannel))
       {
            MessageHeader header = MessageHeader.CreateHeader("PlayerId", "", _playerId);
            OperationContext.Current.OutgoingMessageHeaders.Add(header);
            act(service);
       }

And get the value in server-side:

private long ExtractPlayerIdFromHeader()
    {
        try
        {
            var opContext = OperationContext.Current;
            var requestContext = opContext.RequestContext;
            var headers = requestContext.RequestMessage.Headers;
            int headerIndex = headers.FindHeader("PlayerId", "");
            long playerId = headers.GetHeader<long>(headerIndex);
            return playerId;
        }
        catch (Exception ex)
        {
            this.Log.Error("Exception thrown when extracting the player id from the header", ex);
            throw;
        }
    }
Jiayao
  • 510
  • 3
  • 7
0

The change on WCF side should be as follows:-

MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders; string identity = headers.GetHeader("Identity", "http://www.my-website.com");

I found the same at the following link:-

How to add a custom HTTP header to every WCF call?

The solution works :)

spalMcc
  • 21
  • 4