【问题标题】:How to set WSS-Type in SOAP Service Reference C#如何在 SOAP 服务参考 C# 中设置 WSS-Type
【发布时间】:2019-10-30 09:59:28
【问题描述】:

我正在尝试将 SOAP 服务集成到 WPF - C# 项目中。 我已将.wsdl 文件作为服务参考使用到项目中。 我用SoapUI进行了测试,设置后能够得到响应:

  1. 端点
  2. 用户名
  3. 密码
  4. WSS 类型

我需要将 WSS-Type 设置为:PasswordText。 如果我在SoapUI 中将 WSS-Type 留空,我会返回:

未找到 WS-Security 标头

我现在正试图通过 C# 获得响应,但我不知道在哪里/如何在 C# 中设置 WSS-Type。

到目前为止,这是我的 C# 代码:

MyClient mainClient = new MyClient();
object myRequestObject = ...

// Client Credentials
mainClient.ClientCredentials.UserName.UserName = "Username";
mainClient.ClientCredentials.UserName.Password = "Password";

using (new OperationContextScope(mainClient.InnerChannel))
{    
    SoapAuthenticationHeader.Create(mainClient.ClientCredentials.UserName.UserName, mainClient.ClientCredentials.UserName.Password);
    object mainResponse = mainClient.GetResponse(myRequestObject);
}

public static class SoapAuthenticationHeader
{
    public static void Create(string theUsername, string thePassword)
    {
        try
        {
            // Add a HTTP Soap Header to an outgoing request
            string authorization = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(theUsername + ":" + thePassword));
            HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
            requestMessage.Headers.Add("Authorization", authorization);
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error at 'Create'" + Environment.NewLine + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}

我目前遇到的错误是:

HTTP 请求未经客户端身份验证方案授权 '匿名的'。从服务器收到的身份验证标头是 '基本领域="Spring 安全应用程序"。

远程服务器返回错误:(401) Unauthorized.

我也尝试将 .wsdl 用作 Web 参考,但也没有在其中设置凭据。

任何帮助/建议将不胜感激。

【问题讨论】:

    标签: c# soap service-reference


    【解决方案1】:

    编辑App.config 端点标签看起来像这样就可以了:

      <endpoint address="{URL}" binding="basicHttpBinding" bindingConfiguration="{ClassMethodName}" contract="{ServiceReferenc}.{ClassName}" name="{ClassMethodName}">
        <headers>
          <wsse:Security mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-{UsernameToken}">
              <wsse:Username>{Username}</wsse:Username>
              <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">{Password}</wsse:Password>
              <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">{EncodingType}</wsse:Nonce>
            </wsse:UsernameToken>
          </wsse:Security>
        </headers>
      </endpoint>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多