Take advantage of the best practices to build and host secure WCF services WCF (Windows Communication Foundation) is a secure, reliable, and scalable messaging platform for developing services in .Net. This is the second part in the series of articles on WCF best practices. In the first part of this series, we explored the WCF service design and performance guidelines and the recommended best practices. In this part, we’ll discuss the best practices and strategies in error handling, deployment, and security in WCF. Best practices in WCF hosting and security The four major points that you should consider when implementing security for your WCF services include: authentication, authorization, integrity, and confidentiality. If you are hosting your WCF service in IIS or in a Windows Service, you should use the least privileged account. Note that it is a recommended practice to host your WCF service in IIS. For WCF services that need to execute in an intranet environment, it is a good practice to use Windows authentication. You can take advantage of membership providers when you would like to implement an authentication mechanism that needs user credentials. You should enable WCF Audit and Message Logs to retrieve audit information for monitoring your WCF service. WCF Audit helps you to log the security events of your WCF service — you can use the event viewer to view the security log. The following code snippet illustrates how you can turn on WCF Audit: <behaviors> <serviceBehaviors> <behavior> <serviceSecurityAudit auditLogLocation="Security" suppressAuditFailure="True" serviceAuthorizationAuditLevel="SuccessOrFailure" messageAuthenticationAuditLevel="SuccessOrFailure"/> </behavior> </serviceBehaviors> </behaviors> You can enable message logging in WCF to log incoming messages for your WCF service using the following code snippet: <diagnostics> <messageLogging logEntireMessage="True"></messageLogging> </diagnostics> You should not use temporary certificates to implement security when deploying WCF services in the production environment. Again, I would always prefer to use IIS as a hosting platform to host a WCF service unless you need to use some transport protocol that IIS doesn’t provide support for. You should host WCF service on IIS so that you can leverage IIS features related to security like authentication, certificates, etc. When hosting your WCF service in IIS enable SSL to prevent any phishing attacks. You may also take advantage of OAuth for implementing token based authorization for securing your WCF service. WCF provides a standard set of bindings — you can use NetTcpBinding if you would like to use TCP communication that cross machine boundaries. NetTcpBinding is secure and message packets are signed and encrypted by default. You can also use WSHttpBinding to establish a secure session of communication. WSFederationHttpBinding is a good choice if you would like to implement secure communication in federated security scenerios. Each of the binding types supported in WCF can have the following security modes: None, Transport, Message, Both, TransportWithMessageCredential, and TransportCredentialOnly. Transport and message security in WCF You can secure the communication between your WCF service and client by using either Transport Security or Message Security. While the former is used to enforce security at the transport level, the latter is used to encrypt the message that is passed between the WCF server and the client. Note that the wsHttpBinding in WCF supports Message Security by default. The following code snippet illustrates how you can turn Transport Security on using the configuration file of your WCF service. <bindings> <wsHttpBinding> <binding> <security mode="Transport"></security> </binding> </wsHttpBinding> </bindings> If you would like to use Message Security, use the following statement instead. Error Handling When using WCF in your service layer, you should handle exceptions properly so that the service consumer is communicated with the appropriate error message when an exception occurs. You can write exception blocks to handle runtime errors that occur in your WCF services. Handling exceptions in WCF is a bit tricky though primarily because you are constrained to sending .Net objects over the wire and your WCF service can only send out serialized data. You can actually handle exceptions in WCF in one of these three ways: FaultException — You can leverage fault exceptions in WCF to transmit user friendly error messages when exceptions occur in your WCF service methods. Fault exceptions are thrown by a WCF service when an exception occurs at runtime. IErrorHandler — you can take advantage of this interface to handle WCF exceptions globally. Using returnUnknownExceptionsAsFaults attribute — You can set the returnUnknownExceptionsAsFaults attribute to “True” in the service behavior to ensure that your service method can raise an exception as a SOAP fault automatically. You should include exception details in debug builds only. You should never disclose the stack trace or exception details for WCF services hosted in the production environments — it is always a recommended practice to take advantage of Fault Contracts to transmit custom error messages that might occur in your WCF service. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos