<behaviors> <serviceBehaviors> <behavior name="Myservicebehavior"> <servicemetadata httpsgetenabled="true"policyversion="POLICY15"/> <servicedebug includeexceptiondetailinfaults="true"/> <serviceCredentials> <clientCertificate> <authentication Customcertificatevalida Tortype="MyWS.Security.MyServicesCertificateValidator, MyWS"Certificatevalidationmode="Custom"Revocationmode="NoCheck"/> </clientCertificate> <usernameauthentication usernamepasswordvalidationmode="Custom"Customusernamepasswordvalidatortype="MyWS.Security.MyServicesUsernameValidator, MyWS"/> </serviceCredentials> </behavior> </servicebehaviors></behaviors><servicehosting Environment multiplesitebindingsenabled="true"/><bindings> <basicHttpBinding> <binding name="mysoapbinding"> <security mode="transportwithmessagecredential"> <transport clientcredentialtype="Certificate"/> <message clientcredentialtype="UserName"/> </security> </binding> </basicHttpBinding></bindings><services> <service behaviorconfiguration="Myservicebehavior"Name="MyWS.Services.TheService"> <endpoint address=""binding="BasicHttpBinding"bindingconfiguration="mysoapbinding"Name="Theservice"Bindingnamespace="Https://services.my/TheService"contract="MyWS.Interfaces.Service.ITheService"/> "Https://localhost:4434/MyWS/TheService"/> </baseAddresses> private static Binding createmultifactorauthenticationbinding () {var httpstransport = new Httpstransportbindingelement (); The message security binding element would be configured to require 2 tokens://1) A Username-password encrypted wit H the Service token//2) A client certificate used to sign the message//Create symmetric security binding element With encrypted Username-password token. Symmetric key is encrypted with server certificate. var messagesecurity = securitybindingelement.createusernameforcertificatebindingelement (); Messagesecurity.allowinsecuretransport = false; Require client certificate as endorsing supporting token for all requests from client to server Var Clientx509suppor Tingtokenparameters = new X509securitytokenparameters { Inclusionmode = Securit YtokeninclusionMode.alwaystorecipient}; MESSAGESECURITY.ENDPOINTSUPPORTINGTOKENPARAMETERS.ENDORSING.ADD (clientx509supportingtokenparameters); return new CustomBinding (messagesecurity, httpstransport);}
Registering Wcf-servicesvar returnfaults = new Servicedebugbehavior {includeexceptiondetailinfaults = True};var MetaData = new ServiceMetadataBehavior {httpsgetenabled = True};var servicecredentials = new ServiceCredentials ();//Conf Igure Service SertificateserviceCredentials.ServiceCertificate.SetCertificate (Storelocation.localmachine, Storena Me. My, X509findtype.findbysubjectname, "servercertificate");//Configure Client certificate authentication Modeservice Credentials.ClientCertificate.Authentication.CertificateValidationMode = x509certificatevalidationmode.chaintrust;//ADD Custom Username-password ValidatorserviceCredentials.UserNameAuthentication.UserNamePasswordValidationMode = Usernamepasswordvalidationmode.custom; ServiceCredentials.UserNameAuthentication.CustomUserNamePasswordValidator = _container. Resolve<myservicesusernamevalidator> ();//ADD Custom Certificate ValidatorserviceCredentials.ClientCertificate.Authentication.CertificateValidationMode = X509certificatevalidationmode.custom; ServiceCredentials.ClientCertificate.Authentication.CustomCertificateValidator = _container. Resolve<myservicescertificatevalidator> (); var serviceModel = new Defaultservicemodel (); Servicemodel.addendpoints (Wcfendpoint.forcontract<imycontract> (). Boundto (Createmultifactorauthenticationbinding ())); ServiceModel.BaseAddresses.Add (The New Uri ("https://server.com/ Myserviceimplementation.svc ")); Servicemodel.addextensions (servicecredentials); Servicemodel.addextensions ( MetaData); _container. Addfacility<wcffacility> (f = f.closetimeout = TimeSpan.Zero). Register (Component.for<imycontract> (). Implementedby<myserviceimplementation> (). Aswcfservice (ServiceModel), component.for<iservicebehavior> (). Instance (returnfaults));
WCF Configuration and code creation