Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在SOAP服务参考C中设置WSS类型#_C#_Soap_Service Reference - Fatal编程技术网

C# 如何在SOAP服务参考C中设置WSS类型#

C# 如何在SOAP服务参考C中设置WSS类型#,c#,soap,service-reference,C#,Soap,Service Reference,我正在尝试将SOAP服务集成到WPF-C项目中。 我已经将.wsdl文件作为服务引用使用到项目中。 我已经使用了SoapUI进行了测试,并能够在设置后得到响应: 端点 用户名 密码 WSS类型 我需要将WSS类型设置为:PasswordText。 如果我在SoapUI中将WSS Type留空,我将返回: 未找到WS-Security标头 我现在正试图通过C#获得响应,但我不知道在C#中在何处/如何设置WSS类型 这是我到目前为止的C#代码: MyClient mainClient = new M

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

  • 端点
  • 用户名
  • 密码
  • WSS类型
  • 我需要将WSS类型设置为:
    PasswordText
    。 如果我在
    SoapUI
    中将WSS Type留空,我将返回:

    未找到WS-Security标头

    我现在正试图通过C#获得响应,但我不知道在C#中在何处/如何设置WSS类型

    这是我到目前为止的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请求未经客户端身份验证方案授权 “匿名”。从服务器接收的身份验证标头为 'Basic realm=“Spring安全应用程序”

    远程服务器返回错误:(401)未经授权

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


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

    编辑
    App.config
    endpoint标记,使其看起来像这样,可以实现以下目的:

      <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>
    
    
    {Username}
    {密码}
    {EncodingType}