In this scenario, your users do not have Microsoft Windows® accounts and use a Windows Forms client to connect over the Internet to your WCF service. The business logic called by the WCF service is backed by a Microsoft SQL Server® data store. The basic model for this application scenario is shown in the following figure.
|
Checks / more information
|
Example
|
|
WCF service—configuration
|
|
An ASP.NET database is created for use with the SQL Server membership provider and SQL Server role provider.
Aspnet_regsql.exe creates the SQL Server database to store the user and role information.
|
aspnet_regsql -S .\SQLExpress -E -A r m
|
|
A connection string is configured to point to the user and role stored in SQL Server.
The database connection string includes Integrated Security=SSPI or Trusted Connection=Yes for Windows Authentication.
|
<add name="MyLocalSQLServer" connectionString="Initial Catalog=aspnetdb;data source=localhost;Integrated Security=SSPI;" />
|
|
SqlMembershipProvider is configured to be used with the membership feature.
The membership feature helps protect credentials, can enforce strong passwords, and provides consistent APIs for user validation and secure user management.
|
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider"
connectionStringName="MyLocalSQLServer"
applicationName="MyAppName"
type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>
|
|
The Role Manager feature is enabled and SqlRoleProvider is configured for roles authorization.
Role Manager allows you to look up users’ roles without writing and maintaining custom code.
|
<roleManager enabled="true" defaultProvider="MySqlRoleProvider" >
<providers>
<clear/>
<add name="MySqlRoleProvider"
connectionStringName="MyLocalSQLServer"
applicationName="MyAppName"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>
|
|
The WCF service process identity is given access permissions to the ASP.NET database.
Your WCF service process identity requires access to the aspnetdb database.
|
-- Create a SQL Server login for the Network Service account
sp_grantlogin '<<Custom Service Account>>'
-- Grant the login access to the membership database
USE aspnetdb
GO
sp_grantdbaccess '<<Custom Service Account>>', '<<Custom Service Account>>'
-- Add user to database role
USE aspnetdb
GO
sp_addrolemember 'aspnet_Membership_FullAccess', '<<Custom Service Account>>'
sp_addrolemember 'aspnet_Roles_FullAccess', '<<Custom Service Account >>’
|
|
The WCF service is configured to use wsHttpBinding.
wsHttpBinding uses the HyperText Transfer (HTTP) protocol and provides full support for Simple Object Access Protocol (SOAP) security, transactions, and reliability. Because clients are on the Internet, this is the only choice.
|
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="BindingConfiguration"
name="WsBinding" contract="IService"/>
|
|
WCF service—authentication
|
|
wsHttpBinding is configured to use username authentication and message security.
|
<wsHttpBinding>
<binding name="BindingConfiguration">
<security>
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
|
|
SqlMembershipProvider is configured to provide user authentication.
The membership feature automatically authenticates and creates the authentication ticket for you.
|
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider" connectionStringName="MyLocalSQLServer" applicationName="MyAppName" type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>
|
|
Service behavior is configured to use MembershipProvider for use with username authentication.
|
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="MySqlMembershipProvider" />
|
|
The service behavior is configured to publish metadata.
|
<serviceMetadata httpGetEnabled="true" />
|
|
A service certificate is installed on the WCF service machine. The service behavior is configured to use the service certificate.
This is required for protecting the user credentials in the message.
|
<serviceCertificate
findValue="CN=machine.domain.com" />
|
|
WCF service—authorization
|
|
The service behavior is configured to use AspNetRoles with SqlRoleProvider.
|
<serviceAuthorization principalPermissionMode="UseAspNetRoles"
roleProviderName="MySqlRoleProvider" />
|
|
WCF operations are configured to declaratively perform role checks at the operation level.
Declarative role checks on operations is the preferred mechanism.
|
[PrincipalPermission(SecurityAction.Demand, Role="Managers")]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
|
|
Roles APIs are used to perform programmatic role checks, for fine-grained access control.
If you need finer-grained authorization control, you can use imperative role checks in the code itself. Use a call to Roles.IsUserInRole to perform the check.
|
If(Roles.IsUserInRole(“Manager”))
{
// do something for the manager
}
else
{
// throw an error.
}
|
|
WCF service—SQL
|
|
The connection string for 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");
|
|
Open the database connection by using the WCF process identity’s security context.
The service does not impersonate the original caller to benefit from connection pooling.
| |
|
Checks / more information
|
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 mapped to a database user for the Web application.
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 database.
|
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.
|