简体   繁体   English

C# 使用访问令牌使用 web 服务 wsdl

[英]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.我正在尝试使用 WSDL web 服务,它需要在 header 中发送访问令牌。 However, I keep getting a 401 error and I'm not sure if I am injecting the token correctly.但是,我不断收到 401 错误,我不确定我是否正确注入了令牌。

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.这将返回 401 错误。

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 使用 WCF 的 Http 请求中缺少授权 Header
http://plainoldstan.blogspot.com/2008/07/avoid-http-401-roundtrip-with-adding.html http://plainoldstan.blogspot.com/2008/07/avoid-http-401-roundtrip-with-adding.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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