Hashim's Blogs

Friday, July 30, 2004

Consuming Webservices over HTTPS (SSL)

When Webservices are used, a common concern is security: SOAP messages are transferred in plain text over the network, so anyone with a sniffer could intercept the SOAP message and read it. In my opinion this could happen also to binary data, but probably it requires a little bit more hacker skills. So a solution is to use HTTPS (SSL) instead of HTTP, so the communication is encrypted. To accomplish this, you need to get and install a certificate (issued by a Certificate Authority) on your webserver. In a production environment you would buy a certificate from Verisign or another well known CA, or you would install your own CA, which is a component of Windows Server. If you only want to play with HTTPS, SSL and certificates or your project is in the development phase, you can also generate a test certificate using the MakeCert.exe tool (included in the .NET Framework SDK). After that you have to add this certificate to a website in IIS, and set a port which HTTPS should use.

When you browse to a HTTPS site, you probably get a dialog window asking you if you want to trust the certificate provided by the webserver. So the responsibility of accepting the certificate is handled by the user. Let's get back to the webservice scenario, if you want to invoke a webservice located on a webserver which uses SSL and HTTPS there is a problem. When you make the call from code, there is no dialog window popping up, and asking if you trust the certificate (luckily because this would be pretty ugly in server-side scenarios); probably you'll get following exception:
An unhandled exception of type 'System.Net.WebException' occurred in system.dll

Additional information: The underlying connection was closed: Could not establish trust relationship with remote server.

But there is a solution for this problem, you can solve this in your code by creating your own CertificatePolicy class (which implements the ICertificatePolicy interface). In this class you will have to write your own CheckValidationResult function that has to return true or false, like you would press yes or no in the dialog window. For development purposes I've created the following class which accepts all certificates, so you won't get the nasty WebException anymore:

public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy()
{}

public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert,WebRequest req, int problem)
{
return true;
}
}

As you can see the CheckValidationResult function always returns true, so all certificates will be trusted. If you want to make this class a little bit more secure, you can add additional checks using the X509Certificate parameter for example. To use this CertificatePolicy, you'll have to tell the ServicePointManager to use it:
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
This must be done (one time during the application life cycle) before making the call to your webservice.

Wednesday, July 28, 2004

J2EE vs. Microsoft.NET By Chad Vawter and Ed Roman (Good Article to Read)

J2EE vs. Microsoft.NET
A comparison of building XML-based web services
By Chad Vawter and Ed Roman June 2001
Prepared for Sun Microsystems, Inc.

J2EE vs. Microsoft.NET A comparison of building XML-based web services

In this whitepaper, the authors has made a powerful comparison between the two choices that businesses have for building XML-based web services: the Java 2 Platform, Enterprise Edition (J2EE)1 , built by Sun Microsystems and other industry players, and Microsoft.NET2, built by Microsoft Corporation.

Tuesday, July 20, 2004

Lock the system without using CTRL+ALT+DEL

Hi,

This is a interesting piece of info that you can use to lock your
work-stations without using the combo key "CTRL- ALT- DEL"
If CTRL-ALT-DELETE seems like too much of a hassle, try this instead:
1. Right click an empty spot on the desktop, point to New and click
Shortcut.
2. In the Create Shortcut dialog box, type the following into the Type the
location of the item text box:
"rundll32 user32.dll,LockWorkStation" // remove quotes while typing
3. Click Next.
4. In the Select a Title for the Program dialog box, type "Lock Desktop" in
the Type a name for this shortcut text box.
5. Click Finish.

Simple Encryption and Decryption Using VB.NET

Imports System.Security.Cryptography
Imports System.Text
******* Encrypt the Data *******
Public Function GetEncryptedData(ByVal Data As String) As String
Dim shaM As New SHA1Managed
Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)))
Dim eNC_data() As Byte = ASCIIEncoding.ASCII.GetBytes(Data)
Dim eNC_str As String = Convert.ToBase64String(eNC_data)
GetEncryptedData = eNC_str
End Function
******* Decrypt the Data *******
Public Function GetDecryptedData(ByVal Data As String) As String
Dim dEC_data() As Byte = Convert.FromBase64String(Data)
Dim dEC_Str As String = ASCIIEncoding.ASCII.GetString(dEC_data)
GetDecryptedData = dEC_Str
End Function
*********************************

The above code snippet demonstrates simple encryption/Decryption a given string very use full in password encryption.
The function uses SHA1 to Compute the SHA1 hash for the input data.
The hash is used as a unique value of fixed size representing a large amount of data.
The hash size for the SHA1 algorithm is 160 bits.
This function Imports the  System.Security.Cryptography and System.Text namespace for this.