Chapter 12 - Intranet – Windows Forms to Remote WCF Using Transport Security (Original Caller, TCP)

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

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.

Ch12scenario.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 and 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


Ch12solution.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.

Note: Due to a limitation with tables on this site, the code in the tables below may be run together when in the same cell. If this is a problem for you, please comment the workitem at
http://www.codeplex.com/CodePlex/WorkItem/View.aspx?WorkItemId=17004

Thick Client

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

Application Server

Check & More Info Example
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.


Check & More Info Example
WCF Service - Configuration
Configure the WCF Service to use netTcpbinding. The base address is provided in the configuration section. 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. <endpoint address="" binding="netTcpBinding" bindingConfiguration="" name="TcpBinding" contract= "WCFServicecHost.IMyService"/> <baseAddresses> <add baseAddress= "net.tcp://WCFApp01.npscode.com/MyService" /> </baseAddresses>
A mex endpoint is created for publishing the metadata. This is required so that client can add reference to the WCF Service using SvcUtil utility. <endpoint address="Mex" binding="mexTcpBinding" bindingConfiguration="" name="MexEndpoint" contract="IMetadataExchange" />
Service Metadata is configured in service behavior. The service metadata entry is required for the windows service host to start. HTTP and HTTPS get are disabled. <serviceMetadata />
WCF Service - Authentication
The netTcpBinding is configured to use Windows Authentication and Transport security. By default netTcpBinding is configured to use Windows Authentication and Transport Security. <endpoint address="" binding="netTcpBinding" bindingConfiguration="" />
WCF Service - Authorization
Role Manager feature is enabled and WindowsTokenRoleProvider is configured for roles authorization. Roles authorization can be performed declaratively or imperatively in the operation contract. <serviceAuthorization principalPermissionMode ="UseAspNetRoles" roleProviderName ="AspNetWindowsTokenRoleProvider" />
Perform Role-checks declaratively using Windows Identity Token, for checking Active Directory group membership. Declarative role-checks on operations is the preferred mechanism. PrincipalPermission(SecurityAction.Demand, Role "npscode\\accounting") public string GetData(string message) { return "hello"; }
Perform Role-checks imperatively using Windows Identity Token, for checking Active Directory group membership. 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. public string GetData(string myValue) { if(Roles.IsUserInRole(@"npscode\Accounting")) { //Do something for Accounting role } else { //Do something for non-accounting role or throw an error } }
WCF Service - SQL
The connection string for the database is configured to use Windows authentication. The database connection string includes Integrated Security=SSPI or Trusted Connection=Yes. SqlConnection sqlcon = new SqlConnection("Server=10.3.19.11;Database=Northwind;IntegratedSecurity=SSPI");
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

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

Communication Security

What Check
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$_' 

Additional Resources


Last edited Jun 26, 2008 at 11:45 PM by rboucher, version 16

Comments

No comments yet.