Step 6:- We need to host our service in the IIS. So make the directory as an IIS application so that your service can be hosted. Now if you try to browse the service i.e. the SVC file you will see that it pops up the authentication authorization security dialog box. So this service cannot be executed with windows authentication.
Step 7:- So let’s consume this WCF services. So add an ASP.NET webapplication and do a add webreference. You will be popped up with a dialog box as shown below. Click on add reference so that a proxy is generated for the WCF service.
Step 8:- Type in the following code snippet in your page load. So add the namespace reference and call the method ‘GetData’. The most important step to note is the credential supplied. ‘DefaultCredentials’ passes the current windows identity to the WCF service.
If you execute the service you should get the following display as shown below.
You can try commenting the below code in your client in other words we are not passing any credentials.
obj.Credentials = System.Net.CredentialCache.DefaultCredentials;
Now if you execute you should get the below error stating that this is a unauthorized call.
Whatever code we have discussed in the above FAQ . You can download the Source Code from here
Asked In: Many Interviews |