如何在 SOAP 服务参考 C# 中设置 WSS 类型
How to set WSS-Type in SOAP Service Reference C#
我正在尝试将 SOAP 服务集成到 WPF - C# 项目中。
我已将 .wsdl
文件用作项目的服务参考。
我用SoapUI
测试过,设置后能得到响应:
- 端点
- 用户名
- 密码
- WSS 类型
我需要将 WSS 类型设置为:PasswordText
。
如果我在 SoapUI
中将 WSS-Type 留空,我会返回:
No WS-Security header found
我现在正尝试通过 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);
}
}
}
我目前遇到的错误是:
The HTTP request is unauthorized with client authentication scheme
'Anonymous'. The authentication header received from the server was
'Basic realm="Spring Security Application".
The remote server returned an error: (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>
我正在尝试将 SOAP 服务集成到 WPF - C# 项目中。
我已将 .wsdl
文件用作项目的服务参考。
我用SoapUI
测试过,设置后能得到响应:
- 端点
- 用户名
- 密码
- WSS 类型
我需要将 WSS 类型设置为:PasswordText
。
如果我在 SoapUI
中将 WSS-Type 留空,我会返回:
No WS-Security header found
我现在正尝试通过 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);
}
}
}
我目前遇到的错误是:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="Spring Security Application".
The remote server returned an error: (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>