使用需要C#中的操作级别授权的WSDL

时间:2013-02-28 03:03:15

标签: c# soap visual-studio-2012 wsdl service-provider

我正在尝试使用第三方WSDL。我已将其添加为服务参考。我将客户端初始化并查询这样的参数:

var ltRequest = new SearchEmailAddressStatus 
            {
                 EmailAddress = emailAddressList.ToArray()
            };
var ltClient = new CommunicationPreferenceServiceClient
             {
                ClientCredentials =
                   {
                       UserName = 
                         {
                             UserName = ltProperties.CompanyCredential.UserName,
                             Password = ltProperties.CompanyCredential.Password
                         }
                     }
              };  
var ltResponse = ltClient.searchEmailAddressStatusWS(ltRequest);

在Fiddler中观看数据包后,我注意到Auth标头从未发送到服务器。有没有办法在我的请求中手动插入授权标头?

1 个答案:

答案 0 :(得分:0)

好的,经过大量的挖掘,我找到了答案。在声明客户端后,我使用了以下代码:

using (var scope = new OperationContextScope(ltClient.InnerChannel))
{
    var reqProperty = new HttpRequestMessageProperty();
    reqProperty.Headers[HttpRequestHeader.Authorization] = "Basic " 
             + Convert.ToBase64String(Encoding.ASCII.GetBytes(
               ltClient.ClientCredentials.UserName.UserName + ":" + 
               ltClient.ClientCredentials.UserName.Password));

    OperationContext.Current
        .OutgoingMessageProperties[HttpRequestMessageProperty.Name] = reqProperty;

    var ltResponse = ltClient.searchEmailAddressStatusWS(ltRequest);
}

我相信这是在wsdl请求中获取自定义标头的最不脏的方法。如果有人有更好的方法,我很乐意听到。