Tuesday, August 24, 2021

.NET REST Integration

Of course, I need to cover off the other web services practice, RESTful.

Building the API is pretty simple, using something like https://restunited.com/

Just plug in the JSON requests and let rest united generate all of the entities and clients for you!

Obviously, it is useful to have a guide that describes the RESTful service and provides examples, e.g. https://hotdocsadvance.com/api/rest/documentation/index.html

Then it is just a simple matter of calling the API from your c# code, e.g.

            ServicesApi serviceAPI = new ServicesApi();
            ServiceListResponse ServiceListResponse =
                serviceAPI.SearchServices(headerValue, parameter1, parameter2);
            List<Results> results = ServiceListResponse.Results;

There are a few tips that are useful for setting up the endpoints in rest united, that I will cover off in another post.

Tuesday, July 27, 2021

.NET SOAP Web service Integration

Set up a mock service to test against

Java version

1.      https://www.eclipse.org/downloads/packages/release/2021-03/r/eclipse-ide-enterprise-java-and-web-developers

2.      https://tomcat.apache.org/download-80.cgi 32-bit/64-bit Windows Service Installer (pgp, sha512)

3.      https://stackoverflow.com/questions/30094103/tomcat-configuration-in-eclipse

4.      xsd - How to reference a local XML Schema file correctly? - Stack Overflow

5.      https://stackoverflow.com/questions/43866503/how-to-generate-mock-web-service-using-wsdl-in-java

Setup SSL

https://tomcat.apache.org/tomcat-8.5-doc/ssl-howto.html

cd C:\Program Files\Java\jre1.8.0_251

keytool -genkey -alias tomcat -keyalg RSA -keystore C:\_src\MockServices\Servers\service.jks

Modify tomcat server.xml:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="443"/>

<Connector port="443" protocol="HTTP/1.1"
maxThreads="250" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="C:_src\MockServices\Servers\service.jks"
keystorePass="Password1"
clientAuth="false" sslProtocol="TLS" sslEnabledProtocols="TLSv1.2"
URIEncoding="UTF-8"
/>

 

SOAP XML Integration

Method 1 - WSDL

Alter your WSDL so that it references the XSD in the same directory.

In VS, Tools, Command Line, Developer tools, enter this command

wsdl Service.wsdl Service.xsd /username: xxxxxxxx /password: xxxxxxxx

This will generate a service class.

Put the class in its own project, and add a reference to Microsoft.Web.Services2 using nuget.

Add a reference to system.web.services using Assemblies:


Modify the generated class:

·         Change name of class suffix from ServiceService to ServiceClient (pure aesthetics).

·         Change parent class to Microsoft.Web.Services2.WebServicesClientProtocol

·         Change default URL to a useful value.

·         Add a constructor that accepts the service point URL.

public ServiceClient() {
this.Url = "https://xxx.yyy.zzz/Service";
}

public ServiceClient(string soapServiceURL)
{
this.Url = soapServiceURL;
}


Write a client class that calls the generated code. e.g.

generatedClassforResponse response = getHttpsEndpoint().GetSomething();

 

Security references

The point of using wsdl.exe rather than WCF is so that we can manipulate the SOAP security settings.

private ServiceClient getHttpsEndpoint()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);

binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

CustomBinding customBinding = new CustomBinding(binding);
SecurityBindingElement securityElement = customBinding.Elements.Find<SecurityBindingElement>();
securityElement.IncludeTimestamp = true;

SslStreamSecurityBindingElement sslSecurityElement = new SslStreamSecurityBindingElement();
sslSecurityElement.RequireClientCertificate = false;
customBinding.Elements.Insert(2, sslSecurityElement);

EndpointAddress remoteAddress = new EndpointAddress(soapServiceURL);
ServiceClient client = new ServiceClient(soapServiceURL);

UsernameToken token = new UsernameToken(userName, password, PasswordOption.SendPlainText);
client.RequestSoapContext.Security.Tokens.Add(token);

var proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
ServicePoint sp1 = ServicePointManager.FindServicePoint(new Uri(soapServiceURL), WebRequest.DefaultWebProxy);
client.Proxy = proxy;

return client;
}


These resources were used when looking at security:

https://stackoverflow.com/questions/14869602/add-wsse-soap-header-to-web-reference/67310603#67310603

https://stackoverflow.com/questions/5836685/correct-way-communicate-wsse-usernametoken-for-soap-webservice

https://stackoverflow.com/questions/5833539/how-to-add-security-header-to-a-soap-message

Method 2 - WCF

Do not use this method if you need to adjust the security configuration.

https://medium.com/grensesnittet/integrating-with-soap-web-services-in-net-core-adebfad173fb

The c# project needs to be a Class Library (.NET standard)

right click on the project after it is created and then add a connected service. Enter the wsdl location and the code will then be generated.