Navigation

Sunday 5 May 2013

WCF: Transport layer security implementation


Transport layer security implementation

=>note: keeping in mind that this program is tested on vs 2010 framework 4.0.


step 1:


<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="TP">
<security mode="Transport">
<transport clientCredentialType="Basic">
</transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="TP" contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>

</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Step 2:in Interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

[ServiceContract]
public interface IService
{

[OperationContract]
string GetData();

}

Step 3:In Service class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

public class Service : IService
{
public string GetData()
{
return "DWARIKA PRASAD";
}

}

step4:

open iis
double click on authentication
check Anonymous Authentication is enabled and
basic Authentication is Enabled if not
please Enabled

Step5:Again in iis

double click on server certificate
now click on create self signed certificate
write something in textbox like your name anything
click ok

step 6:again in iis

click on triangle sign of sites
now double click on default web site
select your service(means wcf application name)
right click on default web site
choose Edit binding
now click on add button
choose https from Type and and choose your name from ssl certificate which has write at create time of "create self signed certificate"
click ok

step 7

write following on notepad

netsh http add sslcert ipport=0.0.0.0:8080 certhash=66be7f467713c296ae2fbf70f0bf3c3ae92785e6 appid={2FCB2BB7-AAF7-4BC1-884F-FA12E134D72D}

this is my tested code
but for you
for certhash open again iis
double click on server certificate
now
double click on your certificate which has already created by you
now go to Details tab
choose Thumbprint only
now copy data from below box
paste this on another notepad
choose empty space
press ctrl h
now take one space in find what
click on replace all
now copy this code in certhash in previous notepad

Step 8 :forappid

=>
open vs ide
click on tools
choose create guid
click on guid
choose 4th option and copy data from result
paste this info in appid as above mention

now copy full line netsh wali from step 7
and go on command prompt of vs
in cd\:
paste all code
now press enter
you get following message
"ssl certificate successfull added"

if ssl certificate already exist write followinig on command prompt to delete
netsh http delete sslcert ipport=0.0.0.0:8080

now debug yor wcf application
=> copy the address from url

step9:at client site

take proxy
keeping in mind that your computer should be password protected because
this ask user name and password of your computer

now take a label and a button
on button click write following code

ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
s.ClientCredentials.UserName.UserName = " write username of your computer not pc name";
s.ClientCredentials.UserName.Password = "write yor copmputer password here";
Label1.Text= s.GetData();

=>debug your application

No comments:

Post a Comment