繁体   English   中英

如何在 SOAP 服务参考 C# 中设置 WSS-Type

[英]How to set WSS-Type in SOAP Service Reference C#

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

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

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

未找到 WS-Security header

我现在正试图通过 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 请求未经客户端身份验证方案“匿名”授权。 从服务器收到的身份验证 header 是“基本领域 =“Spring 安全应用程序”。

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

我也尝试使用.wsdl作为 Web 参考,但在那里设置凭据也没有运气。

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

编辑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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM