我有一个 http wsdl 网络服务。我将此服务添加到我在 vs2015 上的 wpf 应用程序中。但是用户名和密码有问题。网络凭据也不起作用。
应用程序抛出“验证消息时遇到安全错误”。当我尝试使用错误的用户名/密码在肥皂 ui 上发送请求时,我收到了这条消息。
public static void createTicket()
{
//NetworkCredential cred = new NetworkCredential(username,password);
CALive.servicesClient client = new CALive.servicesClient();
client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;
client.test("asd");
}
用户名和密码是真实的,它在soapui上工作。我尝试添加为 Web 服务和服务引用,但它们都无法对服务进行身份验证。
定义不同的绑定以更改凭据类型。但没有任何改变。传输安全模式不适用于 http 服务。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="servicesServiceSoapBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" realm=""/>
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://WSDLSERVICE:8080/****/axis/services"
binding="basicHttpBinding" bindingConfiguration="servicesServiceSoapBinding"
contract="CALive.services" name="servicesPort" />
</client>
</system.serviceModel>
我的终点详情;
<wsdl:service name="servicesService">
<wsdl:port binding="tns:servicesServiceSoapBinding" name="servicesPort">
<soap:address location="http://WSDLSERVICE:8080/****/axis/services"/>
</wsdl:port>
</wsdl:service>
我需要人们认为的任何经验。我浪费了几天,真的我无法得到解决方案。
{System.ServiceModel.FaultException: A security error was encountered when verifying the message
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Aragorn.CALive.services.test(test request)
at Aragorn.CALive.servicesClient.Aragorn.CALive.services.test(test request) in C:\Visual Studio 2015\Projects\Monitoring\Monitoring\Service References\CALive\Reference.cs:line 2470
at Aragorn.CALive.servicesClient.test(String test1) in C:\Visual Studio 2015\Projects\Monitoring\Monitoring\Service References\CALive\Reference.cs:line 2476
at Aragorn.CAService.createTicket() in C:\Visual Studio 2015\Projects\Monitoring\Monitoring\CAService.cs:line 27
at Aragorn.MainWindow..ctor() in C:\Visual Studio 2015\Projects\Monitoring\Monitoring\MainWindow.xaml.cs:line 81}
我尝试使用 HttpClient 连接到服务,抛出同样的错误
var TARGETURL = "http://********:8080/****/axis/services";
XmlDocument requestBody = new XmlDocument();
requestBody.LoadXml(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ser='http://service.mn.com/'>
<soapenv:Header/>
<soapenv:Body>
<ser:test>
<test>test</test>
</ser:test>
</soapenv:Body>
</soapenv:Envelope>");
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("http://******:8080"),
UseProxy = true,
};
using (var client = new HttpClient(handler))
{
HttpRequestMessage requestMessage = new HttpRequestMessage()
{
RequestUri = new Uri(TARGETURL+"/test"),
Method = HttpMethod.Post
};
requestMessage.Headers.Add("SOAPAction", TARGETURL+"/test");
requestMessage.Content = new StringContent(requestBody.InnerXml, Encoding.UTF8, "text/xml");
requestMessage.Content.Headers.ContentType.CharSet = Encoding.UTF8.HeaderName;
// ... Use HttpClient.
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.SendAsync(requestMessage);
var content = await response.Content.ReadAsStringAsync();
Debug.WriteLine(content);
}
这里还有一个问题。如果我的密码包含与“* 或!”相同的特殊字符 字符是否试图阻止身份验证?
已解决: 如果soap ui 使用ws-passwordtype is PasswordText 连接到服务,那么我们应该在标题中定义wsse:Security 标签。
<endpoint bla bla bla>
<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse: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:UsernameToken>
</wsse:Security>
</headers>
</endpoint>