C# WPF正在使用服务引用,但我无法获得授权的服务客户端
我有一个http wsdl web服务。我将此服务添加到vs2015上的wpf应用程序中。但是用户名和密码有问题。 网络凭据也不起作用 应用程序正在抛出“验证消息时遇到安全错误”。当我尝试使用错误的用户名/密码在soap ui上发送请求时,我收到了此消息C# WPF正在使用服务引用,但我无法获得授权的服务客户端,c#,wpf,soap,service-reference,C#,Wpf,Soap,Service Reference,我有一个http wsdl web服务。我将此服务添加到vs2015上的wpf应用程序中。但是用户名和密码有问题。 网络凭据也不起作用 应用程序正在抛出“验证消息时遇到安全错误”。当我尝试使用错误的用户名/密码在soap ui上发送请求时,我收到了此消息 public static void createTicket() { //NetworkCredential cred = new NetworkCredential(username,password);
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");
}
用户名和密码为true,它在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>
我尝试使用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);
}
var TARGETURL=“http://********:8080/***/axis/services”;
XmlDocument requestBody=新的XmlDocument();
requestBody.LoadXml(@)
测试
");
HttpClientHandler handler=新的HttpClientHandler()
{
Proxy=新的WebProxy(“http://*****:8080”),
UseProxy=true,
};
使用(var客户端=新的HttpClient(处理程序))
{
HttpRequestMessage requestMessage=新建HttpRequestMessage()
{
RequestUri=新Uri(TARGETURL+“/test”),
方法=HttpMethod.Post
};
添加(“SOAPAction”,TARGETURL+”/test);
requestMessage.Content=newstringcontent(requestBody.InnerXml,Encoding.UTF8,“text/xml”);
requestMessage.Content.Headers.ContentType.CharSet=Encoding.UTF8.HeaderName;
//…使用HttpClient。
var byteArray=Encoding.ASCII.GetBytes(“用户名:密码”);
client.DefaultRequestHeaders.Authorization=newauthenticationheadervalue(“Basic”,Convert.ToBase64String(byteArray));
HttpResponseMessage response=wait client.SendAsync(requestMessage);
var content=await response.content.ReadAsStringAsync();
Debug.WriteLine(内容);
}
这里还有一个问题如果我的密码具有与“*或!”相同的特殊字符,chars是否尝试阻止身份验证?
已解决:
如果SOAPUI使用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>
用户名
密码
有什么帮助吗?问题未解决是使用WCFTestClient.exe加载wsdl并查看它生成的服务模型配置。这可能会有帮助,没有变化。WCFTestClient具有相同的错误。我想我缺少了一些身份验证的属性。Soap UI如何成功连接,Soap UI正在使用哪些参数或属性
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);
}
<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>