How to use a service link with WCF SOAP Service Basic Authentication

I have a WCF SOAP service that uses basic access authentication. SSL is not used - I understand the security issues here.

Using the WCFTestClient application, I confirmed that the service works with temporary hardcoding in the service, username and password to use if the Authorization header is missing.

Now I'm trying to write a test application that passes credentials through the Authorization header. I added a service link to my service in my test application, but the Authorization header is not in the HTTP request. The generated class MyServiceClient uses System.ServiceModel.ClientBase

In my test application, I set credentials as follows

 MyServiceClient client = new MyServiceClient("BasicHttpBinding_MyService"); client.ClientCredentials.UserName.UserName = "WebServiceUsername"; client.ClientCredentials.UserName.Password = "WebServicepassword"; 

I also tried as follows

 MyServiceClient client = new MyServiceClient(); ClientCredentials loginCredentials = new ClientCredentials(); loginCredentials.UserName.UserName = "WebServiceUsername"; loginCredentials.UserName.Password = "WebServicepassword"; client.Endpoint.Behaviors.Remove(client.Endpoint.Behaviors.Find<ClientCredentials>()); client.Endpoint.Behaviors.Add(loginCredentials); 

The web.config service is as follows

 <services> <service name="MyService" behaviorConfiguration="MyBehavior" > <endpoint contract="MyService" binding="basicHttpBinding" /> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> </service> </services> 

The app.config test is as follows

 <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_MyService"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Basic"/> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:55314/MyService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyService" contract="MyService" name="BasicHttpBinding_MyService" /> </client> </system.serviceModel> </configuration> 

Any thoughts on what I'm missing?

+6
source share
2 answers

BasicHttpBinding has no security property in WP8, I am very upset that I am trying to access the sharepoint list in WP8. Xamarin iOS / Android is not a problem.

+2
source

This is a good starting point, move the binding and endpoint information from the configuration file to your class:

  protected BasicHttpBinding binding = new BasicHttpBinding() { Name = "Name your binding here", CloseTimeout = new TimeSpan(0, 1, 0), OpenTimeout = new TimeSpan(0, 1, 0), ReceiveTimeout = new TimeSpan(0, 10, 0), SendTimeout = new TimeSpan(0, 1, 0), AllowCookies = false, BypassProxyOnLocal = false, HostNameComparisonMode = HostNameComparisonMode.StrongWildcard, MaxBufferSize = 65536, MaxBufferPoolSize = 524288, MaxReceivedMessageSize = 65536, MessageEncoding = WSMessageEncoding.Text, TransferMode = TransferMode.Buffered, UseDefaultWebProxy = true, Security = new BasicHttpSecurity() { Mode = BasicHttpSecurityMode.Transport, Message = new BasicHttpMessageSecurity() { AlgorithmSuite = SecurityAlgorithmSuite.Default, ClientCredentialType = BasicHttpMessageCredentialType.UserName}, Transport = new HttpTransportSecurity() { ClientCredentialType = HttpClientCredentialType.Digest } }, }; protected EndpointAddress endPoint = new EndpointAddress("http://localhost:55314/MyService.svc"); 

and then

 MyServiceClient client = new MyServiceClient(binding, endpont); 

Try this and customize the binding to your needs, especially Security.

+1
source

Source: https://habr.com/ru/post/911438/


All Articles