1

I'm trying to consume a WSDL web service and it requires an access token to be sent in the header. However, I keep getting a 401 error and I'm not sure if I am injecting the token correctly.

Heres a snippet of the code:

 var client = new WsldClient(); 
 var operationContext = new OperationContext(client.InnerChannel); 
 using (new OperationContextScope(operationContext))
 {
     var httpRequestProperty = new HttpRequestMessageProperty();
     httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Bearer " + accessToken
     operationContext.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

     client.SomeMethod();
 }

This returns a 401 error.

Kevin Jones
  • 419
  • 1
  • 6
  • 21

1 Answers1

0

You can try the following code:

var client = new MyClient();
client.ClientCredentials.UserName.UserName = "username";  
client.ClientCredentials.UserName.Password = "password";
var httpRequestProperty = new HttpRequestMessageProperty(); 
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +   Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
var context = new OperationContext(ormClient.InnerChannel); 
using (new OperationContextScope(context)) 
{
      context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
      return await client.SomeMethod(); 
}

Authorization Header is missing in Http request using WCF
http://plainoldstan.blogspot.com/2008/07/avoid-http-401-roundtrip-with-adding.html

Lan Huang
  • 613
  • 2
  • 5