Intranet – Windows Forms to Remote WCF Using Transport Security (TCP)

- J.D. Meier, Jason Taylor, Prashant Bansode, Carlos Farre, Madhu Sundararajan, Steve Gregersen

Applies To

  • Microsoft® Windows Communication Foundation (WCF) 3.5

Scenario

In this scenario, your users have Windows accounts and use a Windows Forms client to connect over the intranet to your WCF Service. The business logic called by the WCF Service is backed by a Microsoft SQL Server® data store. The following figure illustrates the basic model for this application scenario.

Scenario4.jpg

Key Characteristics

This scenario applies to you if:
  • Your users have Windows Forms clients.
  • Your user accounts are in Active Directory within a domain.
  • Your user roles are Windows Groups.
  • Your application transmits sensitive data over the network that needs to be protected.
  • A high-performance connection between the ASP.NET application and the WCF Service is more important than the ability to host the WCF Service in IIS.

Solution


Solution4.jpg

Solution Summary Table

In this solution you will:
  • Use domain credentials to authenticate clients against an Active Directory user store.
  • Use a service account to call the SQL Server from WCF.
  • Use transport security to protect sensitive data between the Windows Forms client and the WCF Service.
  • Use netTcpBinding to support the TCP transport for improved performance.
  • Host WCF in a Windows Service since IIS does not support the TCP transport.

Thick Client

What Checks Example More Info
WCF Proxy
Application has a proxy reference to the WCF Service. WCFTestService.MyServiceClient proxy = new WCFTestService.MyServiceClient(); The application has access to the WCF metadata to create a service reference.
Proxy invokes services with the security context of the logon user. proxy.GetData("data"); proxy.Close(); The proxy will automatically invoke WCF operations using the security context of the current user.

Application Server

What Checks Example More Info
Windows Service
Configuration Windows Service is configured to run under a custom domain service account. Use a domain account if possible.
WCF Service is hosted in a Windows Service. Since IIS does not support netTcpBinding, host in a Windows service instead.
WCF Service
Configuration Configure the WCF Service to use netTcpbinding. The base address is provided in the configuration section. <endpoint address="" binding="netTcpBinding" bindingConfiguration="" name="TcpBinding" contract="WCFServicecHost.IMyService" /> <baseAddresses> <add baseAddress="net.tcp://WCFApp01.npscode.com/MyService" /> </baseAddresses> NetTcpBinding uses the TCP protocol and provides full support for SOAP security, transactions, and reliability. As client and WCF Service both are in intranet this is a good choice from a performance perspective.
A mex endpoint is created for publishing the metadata. <endpoint address="Mex" binding="mexTcpBinding" bindingConfiguration="" name="MexEndpoint" contract="IMetadataExchange" /> This is required so that client can add reference to the WCF Service using SvcUtil utility.
Service Metadata is configured in service behavior. <serviceMetadata /> The service metadata entry is required for the windows service host to start. HTTP and HTTPS get are disabled.
Authentication The netTcpBinding is configured to use Windows Authentication and Transport security. <endpoint address="" binding="netTcpBinding" bindingConfiguration=" " By default netTcpBinding is configured to use Windows Authentication and Transport Security.
Authorization Role Manager feature is enabled and WindowsTokenRoleProvider is configured for roles authorization. <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="AspNetWindowsTokenRoleProvider" /> Roles authorization can be performed declaratively or imperatively in the operation contract.
Perform Role-checks declaratively using Windows Identity Token, for checking Active Directory group membership. [PrincipalPermission(SecurityAction.Demand, Role "npscode\\accounting")] public string GetData(string message){ return "hello";} Declarative role-checks on operations is the preferred mechanism.
Perform Role-checks imperatively using Windows Identity Token, for checking Active Directory group membership. if(Roles.IsUserInRole(@"npscode\Accounting")) { //Do something for Accounting role } else { //Do something for non-accounting role or throw an error } If you need finer-grained authorization control, you can use imperative role checks in the code itself. Use call to Roles.IsUserInRole to perform the check.
SQL The connection string for the database is configured to use Windows authentication. SqlConnection sqlcon = new SqlConnection("Server=10.3.19.11;Database=Northwind;IntegratedSecurity=SSPI"); The database connection string includes Integrated Security=SSPI or Trusted Connection=Yes.
Database connection is opened using the WCF process identity’s security context. Service does not impersonate the original caller to benefit for connection pooling.

Database Server

What Check Example More Info
Configuration A SQL Server login is created for the WCF’s service account (process identity). exec sp_grantlogin 'Custom Service Account' This grants access to the SQL Server.
The login is granted access to the target database. use targetDatabase go exec sp_grantdbaccess 'Custom Service Account' go This grants access to the specified database.
A database role is created in the target database. use targetDatabase go exec sp_addrole 'DB Role Name' go This allows access control and authorization to the DB.
The login is added to the database role. use targetDatabase go exec sp_addrolemember 'DB Role Name', 'Custom Service Account' go Grant minimum permissions. For example, grant execute permissions to selected stored procedures and provide no direct table access.
Authentication SQL Server is configured to use Windows authentication.

Communication Security

What Check Example More Info
App server to Database You can use IPSec or SSL between App server and database server to protect sensitive data on the wire.

Analysis

Thick Client

WCF Proxy

  • Because WCF requires the original user’s credentials for Authentication and Authorization, the original user’s security context makes all calls through the WCF proxy and into the WCF Service.

Application Server

Authentication

  • In order to authenticate the original users when the Thick Client makes calls on the WCF Service, WCF is configured to use Windows Authentication.

Authorization

  • For coarse grained access control, authorization checks are performed declaratively in the WCF Service at the operation level.
  • For fine grained access control, authorization checks are performed programmatically within the operations.
  • The Roles Manager is a good choice for this scenario because it allows your service code to look up users' roles without writing and maintaining custom code.

SQL

  • To reduce the risk of stolen database credentials, the database connection string is configured to use Windows authentication. This avoids storing credentials in files and passing credentials over the network to the database server.
  • The WCF Service accesses the database using the WCF process identity. As a result, all calls use the single process account and the designated database connection pooling.

Configuration

  • This scenario is optimized around transmission performance at the expense of interoperability with clients that expect a legacy Web service and the ability to host the service in IIS. For this reason the best binding choice is netTcpBinding. By default, netTcpBinding supports Windows Authentication with Transport security.
  • Because netTcpBinding is not supported by IIS 6.0, the WCF Service is hosted in a Windows service.
  • In order to reduce attack surface and minimize the impact of a compromise, the Windows service is running under the security context of the Service account using a least privileged account.
  • In order to make it possible for the client to generate a proxy based on the service definition, we’ve exposed a metadata exchange (mex) endpoint.

Database Server

  • SQL Server database user roles are preferred to SQL Server server application roles to avoid the associated password management and connection pooling issues associated with the use of SQL application roles. Applications activate SQL application roles by calling a built-in stored procedure with a role name and a password. Therefore, the password must be stored securely. Database connection pooling must also be disabled when you use SQL application roles, which severely impacts application scalability.
  • Creating a new user-defined database role, and adding the database user to the role, lets you give specific minimum permissions to the role. Therefore, if the database account changes you don't have to change the permissions on all database objects.

Communication Security

  • Transport security protects sensitive data between Thick Client and WCF Service.
  • You can use IPSec or SSL between WCF Service and the database server to protect sensitive data on the wire.

Example

Application Server

Code

The service performs imperative authorization checks calling Roles.IsUserInRole.
The service calls SQL using windows authentication.
using System.Data.SqlClient;
using System.Web.Security;


public string GetData(string myValue)
{
           
*       if(Roles.IsUserInRole(@"npscode\Accounting"))*
       {
*           SqlConnection sqlcon = new SqlConnection("Server=10.3.19.11;Database=Northwind;IntegratedSecurity=SSPI");*
           sqlcon.Open();
           //do the business operation
           return "Authorization succeeded ";
        }
        else
            return "authorization failure";
        }

}

Configuration

  • The service has a binding endpoint that uses netTcpbinding with no binding configuration and windows authentication (default settings).
  • The service has a mex endpoint to publish metadata.
  • The service has a service behavior configuration to expose the role provider to the WCF Service.
  • The service has a base address configured to reduce the size of the binding addresses in the config.
  • The service configuration file has an entry for the AspNetWindowsTokenRoleProvider under system.web to define which role provider is being used.
  • The service behavior is configured with the element serviceAuthorization to allow WindowsTokenRoleProvider as the authorization provider.
  • The service behavior is configured with the element serviceMetadata to allow metadata exposure.
<system.web>
    <roleManager enabled="true"
             defaultProvider="AspNetWindowsTokenRoleProvider" />
  </system.web>
  
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="BehaviorConfiguration">
                    <serviceAuthorization principalPermissionMode="UseAspNetRoles"
                        roleProviderName="AspNetWindowsTokenRoleProvider" />
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings />
        <services>
            <service behaviorConfiguration="BehaviorConfiguration" name="WCFServicecHost.MyService">
                <endpoint address="Mex" binding="mexTcpBinding" bindingConfiguration=""
                    name="MexEndpoint" contract="IMetadataExchange" />
                <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
                    name="TcpBinding" contract="WCFServicecHost.IMyService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://perfpres02.npscode.com/MyService" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>

Database Server

Configuration

  • A SQL Server login is created for the WCF Service account.
  • The WCF login name is given access to the application database.
  • The role is created in the application database.
  • The WCF login name is added to the role.
-- Create a SQL Server login  that matches the WCF machine name
EXEC SP_GRANTLOGIN 'npscode\perfpres02$'

-- Grant the login access to the application database
use testdb 
go 
exec sp_grantdbaccess 'npscode\perfpres02$' 

-- Create the new database role
use testdb
go
exec sp_addrole 'myrole2','db_owner' 

-- Add the new login to the role
use testdb
go
exec sp_addrolemember 'myrole2','npscode\perfpres02$' 

Contributors and Reviewers

  • External Contributors and Reviewers:
  • Microsoft Consulting Services and PSS Contributors and Reviewers:
  • Test team: Rohit Sharma, Chaitanya Bijwe, Parameswaran Vaideeswaran.
  • Edit team: Dennis Rea.
  • SEO team: Rob Boucher.

Last edited Feb 15, 2009 at 11:31 PM by mycodeplexuser, version 2

Comments

No comments yet.