Web service
RESTful Web Services Tutorial with REST API Example
What is Restful Web Services? Restful Web Services is a lightweight, maintainable, and scalable...
WS Security is a standard that addresses security when data is exchanged as part of a Web service.This is a key feature in SOAP that makes it very popular for creating web services.
Security is an important feature in any web application. Since almost all web applications are exposed to the internet, there is always a chance of a security threat to web applications. Hence, when developing web-based applications, it is always recommended to ensure that application is designed and developed with security in mind.
In this tutorial, you will learn-
To understand security threats which can be hostile to a web application, let's look at a simple scenario of a web application and see how it works in terms of Security.
One of the security measures available for the HTTP is the HTTPS protocol. HTTPS is the secure way of communication between the client and the server over the web. HTTPS makes use of the Secure Sockets layer or SSL for secure communication. Both the client and the server will have a digital certificate to identify themselves as genuine when any communication happens between the client and the server.
In a standard HTTPS communication between the client and the server, the following steps take place
But the above type of security will not work in all situations. There can come a time when the client can talk to multiple servers. An example given below shows a client talking to both a database and a web server at a time. In such cases, not all information can pass through the https protocol.
This is where SOAP comes in action to overcome such obstacles by having the WS Security specification in place. With this specification, all security related data is defined in the SOAP header element.
The header element can contain the below-mentioned information
In a multiple server environments, the above technique of SOAP authentication helps in the following way.
We will see in the subsequent topics on how the WS Security standard can be used for SOAP.
As discussed in the earlier section, the WS-Security standard revolves around having the security definition included in the SOAP Header.
The credentials in the SOAP header is managed in 2 ways.
First, it defines a special element called UsernameToken. This is used to pass the username and password to the web service.
The other way is to use a Binary Token via the BinarySecurityToken. This is used in situations in which encryption techniques such as Kerberos or X.509 is used.
The below diagram shows the flow of how the security model works in WS Security
Below are the steps which take place in the above workflow
The below snippet shows the format of the authentication part which is part of the WSDL document. Now based on the below snippet, the SOAP message will contain 2 additional elements, one being the Username and the other being the Password.
<xs:element name="UsernameToken"> <xs:complexType> <xs:sequence> <xs:element ref="Username"/> <xs:element ref="Password" minOccurs="0"/> </xs:sequence> <xs:attribute name="Id" type="xs:ID"/> </xs:complexType></xs:element>
When the SOAP Message is actually passed between the clients and the server, the part of the message which contains the user credentials could look like the one shown above. The wsse element name is a special element named defined for SOAP and means that it contains security based information.
Now let's look at SOAP web service security example. We will build a web service security upon the example demonstrated earlier in the SOAP chapter and will add a security layer to it.
In our example, we are going to create a simple web service, which will be used to return a string to the application which calls the web service. But this time, around, when the web service is invoked, the credentials need to be supplied to the calling service. Let's follow the below steps to create our SOAP web service and add the security definition to it.
Step 1) The first step is to create an empty Asp.Net Web application. From Visual Studio 2013, click on the menu option File->New project.
Once you click on the New Project option, Visual Studio will then give you another dialog box for choosing the type of project and to give the necessary details of the project. This is explained in the next step
Step 2) In this step,
Once done you will see the project file created in your solution explorer in Visual Studio 2013.
Step 3) In this step,
We are going to add a Web service file to our project
The above step will prompt a dialog box,wherein one can enter the name of the web service file. So in the below dialog box, enter the name of TutorialService as the file name.
Step 4) Add the following code to your Tutorial Service asmx file. The below snippet of code is used to add a custom class which will be used to change the SOAP Header when the SOAP message is generated. Since we now want to add security credentials to the SOAP header, this step is required.
return "This is a gtupapers Web Service";
}
public class AuthHeader : SoapHeader
{
public string UserName;
public string Password;
}
}
Code Explanation:-
Step 5) As the next step, the following code needs to be added to the same TutorialService.asmx file. This code actually defines the function of our web service. This function returns a string "This is a gtupapers Web service" to the client. But this time, the string will only be returned if the client application passes the credentials to the web service.
public class TutorialService : System.Web.Services.WebService
{
public AuthHeader Credentials;
[SoapHeader("Credentials")]
[WebMethod]
public string gtupapersWebService()
{
if (Credentials.UserName.ToLower() != "gtupapers" ||
Credentials.Password.ToLower() != "gtupapersPassword")
{
throw new SoapException("Unauthorized",
SoapException.ClientFaultCode);
}
eise
return "This is a gtupapers Web service";
}
Code Explanation:-
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
The above output is shown when the program is run, which means that the Web service is now available. Let's click on the Service Description link.
From the service description, you will now be able to see that the username and password are elements of the WSDL file. These parameters need to be sent when the web service is invoked.
Following are the security considerations which should be noted when working with Web services
Auditing and Log management – Use application logging to log all requests, which comes to the web services. This gives a detailed report on who has invoked the web service and can help in Impact analysis if any security breach occurs.
Flow of calls to the web service – Try to note the flow of the calls in web services. By default, an application could call multiple web services request with Authentication tokens passed between these web services. All calls between web services need to be monitored and logged.
Sensitive Information - Do not include sensitive information in your log entries such as passwords or credit card numbers or any sort of other confidential information. If there is an event which has any of this information, it needs to be discarded before logging.
Track Business Operations - Track significant business operations. For example, instrument your application to record access to particularly sensitive methods and business logic. Let's take an example of an online shopping application. There are multiple steps in a typical application such as the choosing the items to be purchased, the items loaded in the cart and then the final purchase. This entire business workflow needs to be tracked by the web service.
Proper Authentication - Authentication is the mechanism by which the clients can establish their identity with the web service using a certain set of credentials that can prove that identity. One should never store the user credentials, and hence, if WS Security is used to call the web service, it has to be noted that the web service should not store the credentials which are sent in the SOAP header. These should be discarded by the web service.
Summary
What is Restful Web Services? Restful Web Services is a lightweight, maintainable, and scalable...
What is JSON? JSON is used to store information in an organized, and easy-to-access manner. Its...
What is an API? Application Programming Interface(API) is a software interface that allows two...
{loadposition top-ads-automation-testing-tools} What is Service Virtualization? Service...
What is SOAP? SOAP is an XML-based protocol for accessing web services over HTTP. It has some...
Download PDF 1) Explain microservices architecture Microservice Architecture is an architectural...