简体   繁体   中英

C# Consuming web service wsdl with access token

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM