|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionMany of us want ot secure the calls to our web services, right? There are so many ways to do this, one of them is to use custom SOAP header. Using this method we simply add a required SOAP header to our web services calls. We embed the SOAP header into our message and validate its contents on the server. If the SOAP header validation done successfully, the web server sends the web service response to the consumer. Pretty simple, right? Using the codeNow let’s see how to do this in visual studio: /// Notice that the “WebServiceBinding” attribute has the “Name” argument set to “TestService”, I’ll explain this later. Now, I write the custom SOAP header that I want to include in the SOAP message. public class UserCredentials : System.Web.Services.Protocols.SoapHeader { public string userName; public string password; } Let’s add instance from that header in our service: public class SOAPHeaderService : System.Web.Services.WebService { // Visual studio will append a "UserCredentialsValue" property to the proxy class public UserCredentials consumer; Note that the visual studio will create a property in web service proxy called “UserCredentialsValue” which will map the “consumer” public property in the web service. Now we had to write a “Web Method” that uses that header in messaging. [WebMethod]
[SoapDocumentMethod(Binding = "TestService")]
[SoapHeader("consumer",Required=true)]
public string GetBalance()
{
if (checkConsumer())
return consumer.userName + " had 10000000 credit";
else
return "Error in authentication";
}
private bool checkConsumer()
{
// In this method you can check the username and password
// with your database or something
// You could also encrypt the password for more security
if (consumer != null)
{
if (consumer.userName == "Ahmed" && consumer.password == "1234")
return true;
else
return false;
}
else
return false;
}
Note that I have added the “Binding” value to that I had used in declaring my service. Also I declared the SOAP header that method will require when called, as long as declaring it with required. Now, the only thing is remaining is to call the service with the SOAP header: SOAPHeaderService.SOAPHeaderService service = new SOAPHeaderService.SOAPHeaderService(); SOAPHeaderService.UserCredentials user = new SOAPHeaderService.UserCredentials(); user.userName = "Ahmed"; user.password = "1234"; service.UserCredentialsValue = user; Console.WriteLine(service.GetBalance()); We just get reference to the service and the SOAP header, assign the SOAP header properties, attach it with the SOAP message and then make our call to the web method. This is the console result after calling the service with username = “Ahmed” and password = “1234” This one with other data: Points of InterestSecuring their web services is a thing that many developers ignore while they are working; they relay that on that is a difficult and nasty task. In the fact securing web service is all about understand the messaging layer and the protocols, you just need to go a little more deep and then you will find it is a very simple task. I’ll post ISA about the other techniques to secure web services.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||