簡體   English   中英

C# 使用訪問令牌使用 web 服務 wsdl

[英]C# Consuming web service wsdl with access token

我正在嘗試使用 WSDL web 服務,它需要在 header 中發送訪問令牌。 但是,我不斷收到 401 錯誤,我不確定我是否正確注入了令牌。

下面是代碼片段:

 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();
 }

這將返回 401 錯誤。

您可以嘗試以下代碼:

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(); 
}

使用 WCF 的 Http 請求中缺少授權 Header
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