【问题标题】:Not able To pass username and password in header of soapui in wcf service无法在 wcf 服务中的 soapui 标头中传递用户名和密码
【发布时间】:2019-02-13 10:11:11
【问题描述】:

我有一个 WCF 服务,我正试图通过 C# 中的测试客户端项目来使用它,

我正在使用 SOAPUI 应用程序,然后从该应用程序中我能够在请求中传递正文以及身份验证并能够对其进行解码。

但我能够通过测试客户端项目传递soap正文部分,但无法通过请求中的标题和正文部分的身份验证。

我在测试客户端项目中编写的以下代码:

AService.AServiceClient client = new AService.AServiceClient();
        AService.GetAByKeyRequest request = new AService.GetAByKeyRequest
        {
           Authorization = "xyz:123",
           AKey = "123"
        };
        var SoapResponse = ((AService.IAService)client).GetAByKey(request);

下面是请求被消费的部分代码:

GetAResponse GetAByKey(GetAByKeyRequest getAByKeyRequest)
{
    //...
    string basicAuthorization = request.Headers[System.Net.HttpRequestHeader.Authorization];
    //...
}

请给点意见

【问题讨论】:

    标签: c# unit-testing wcf soap soapui


    【解决方案1】:

    不清楚SoapUI测试客户端,但是如果你使用的是wcf客户端,你可以通过HttpRequestMessageProperty添加http头。

     MetadataTestClient client = new MetadataTestClient();
            try
            {
                using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
                {
                    HttpRequestMessageProperty property;
                    if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name)){
                        property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                    }
                    else
                    {
                        property = new HttpRequestMessageProperty();
    //HttpRequestMessagProperty is an item in OutgoingMessageProperties
                        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;
                    }
    
    // add property to HttpRequestMessagePropery could set HttpHeader
                    property.Headers.Add (System.Net.HttpRequestHeader.Authorization, "myAuthorization");
                    string re = client.HelloWorld();
                }
    
    
            }
    

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      相关资源
      最近更新 更多