简体   繁体   中英

How to set Password Type in C# while consuming a SOAP web service

I am trying to consume a third-party SOAP web service in my C# console app. I tried two different way of passing the credentials as shown below.

//Method 1 - passing security credentials
myCardService.Credentials = new System.Net.NetworkCredential("username", 
"password");

//Method 2 - passing security credentials
System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();
NetworkCredential netCred = new NetworkCredential("username", "password");
myCredentials.Add(new Uri(myCardService.Url), "Basic", netCred);
myCardService.Credentials = myCredentials;

In both the cases, I am getting this exception:

System.Web.Services.Protocols.SoapHeaderExceptions: Error in SecurityHeader: An Invalid security token was provided

When I tried SOAP UI to consume the service, there also I got the same error. However, when I made WSS-Password Type = PasswordText in the Properties window of the tool, then it worked. I don't see any way to make this setting in the credential objects in the C# code. Any help is greatly appreciated.

My understanding, that after you define your service binding to the authentication type.

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

After you have defined the authentication type, you would create your web service client and do the following:

client.ClientCredentials.Username.Username = networkCredential.Username;
client.ClientCredentials.Password.Password = networkCredential.Password;

That should be the appropriate manner to pass those credentials.

https://www.microsoft.com/zh-cn/download/details.aspx?id=14089安装WSE 3.0并将Web参考从System.Web.Services.Protocols.SoapHttpClientProtocol手动更改为WSE后,此问题已解决。 Reference.cs文件中的Microsoft.Web.Services3.WebServicesClientProtocol。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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