Enterprise Services Security
Serviced components benefit from a set of authentication and authorization services that are included in the Enterprise Services (COM+) infrastructure. Authentication is provided over the RPC channel between client and server, so clients will automatically present to the server with their Windows identities. Authorization is provided by COM+ security roles, which rely on Windows accounts. An Enterprise Services role can contain Windows users and Windows groups. You can limit access to applications, components, interfaces, and methods within a serviced component to members of specific roles.
In general, you can manage security for serviced components through a combination of attributes within your .NET project, and the Component Services tool, as shown in Step-by-Step 11.10.
|
11.10 Implementing Security for a Serviced Component
Add a new Visual C# Class library project to the solution, named InventoryService. Right-click the Class1.cs file and rename it to Inventory.cs. Right-click the References folder and select Add Reference. Select the System.EnterpriseServices reference from the .NET tab of the Add Reference dialog box and click OK. Add code to create the Inventory class as a serviced component:
using System;
using System.EnterpriseServices;
namespace InventoryService
{
public interface ISell
{
void Sell(int intAmount);
}
[ComponentAccessControl(),
SecureMethod(),
SecurityRole("InventoryUsers")]
public class Inventory
: ServicedComponent, ISell
{
public Inventory() {}
[AutoComplete(),
SecurityRole("InventoryUsers")]
public void Sell(int intAmount)
{
// Work of the component
// would go here
}
}
}
Open a Visual Studio .NET command prompt. Navigate to the directory containing the InventoryService project and create a key file by entering this command:
sn –k InventoryService.snk
Open the AssemblyInfo.cs file. Add the following using directive:
using System.EnterpriseServices;
Add assembly-level attributes ApplicationName, Description, and ApplicationActivation in the AssemblyInfo.cs as follows:
[assembly: ApplicationName("InventoryComponent")]
[assembly: ApplicationAccessControl(
AccessChecksLevel=
AccessChecksLevelOption.ApplicationComponent)]
[assembly: SecurityRole("InventoryUsers")]
Change the AssemblyVersion and AssemblyKeyFile attribute in the AssemblyInfo.cs file as shown here:
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyKeyFile(
@"..\..\InventoryService.snk")]
Build the project to create InventoryService.dll. Switch to the Visual Studio .NET command prompt and navigate to the project's bin\Debug directory. Enter this command to register the assembly with COM+:
regsvcs InventoryService.dll
Add a new Visual C# .NET Windows application project to the solution. Name the new project InventoryClient. Set the new project as the startup project for the solution. Right-click the References folder and select Add Reference. Add references to the System.EnterpriseServices component and InventoryService project. In the Solution Explorer, rename the default Form1.cs to InventoryClient.cs. Open the form in code view and change all occurrences of Form1 to refer to InventoryClient instead. Place a Label control, a TextBox control (txtAmount), and a Button control (btnSell) on the default form in the project. Switch to the code view and add the following using directive:
using InventoryService;
Double-click the Button control and add code to handle the button's Click event:
private void btnSell_Click(
object sender, System.EventArgs e)
{
try
{
Inventory inv = new Inventory();
MessageBox.Show("Sale succeded");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,
"Exception Occured");
}
}
Run the solution, enter a number in the textbox, and click the button. You'll receive an exception message as shown in Figure, because you're not a member of the InventoryUsers COM+ role. Close the form.

Launch the Component Services tool from the Administrative Tools section of the Windows Control Panel. In the Component Services tool, expand the tree to locate the Users node for the InventoryUsers role, as shown in Figure. Right-click the node and select New, User. In the Select Users or Groups dialog box, locate your own user account and click Add, and then click OK.

Run the solution again, enter a number in the text box, and click the button again. This time the call will succeed because your account is a member of the proper COM+ role.
|
Step-by-Step 11.10 shows the essential attributes that you can use to configure security for a serviced component:
The ApplicationAccessControl attribute at the assembly level enables access checking at the process and component level. The SecurityRole attribute at the assembly level specifies the COM+ roles that will be able to use any of the classes from the assembly. The ComponentAccessControl attribute at the class level enables component-level access checking. The SecurityRole attribute at the class level specifies the COM+ roles that will be allowed to create instances of the class. The SecureMethod attribute at the class level allows you to use the Component Services tool to configure roles for the class. The SecurityRole attribute at the method level specifies the COM+ roles that will be allowed to invoke the method.
The most important security decision you must make with a Windows service is which security account should be used to run the service. You can choose between the LocalSystem account or a user account on Windows 2000. On Windows XP, the LocalService and NetworkService accounts provide additional flexibility. Because Web services are hosted by ASP.NET, they have the entire spectrum of IIS authentication, authorization, and security features available to them. You can use IIS to control access to a Web service, and then use declarative or imperative security to control the Web service's access to resources. WS-Security is a new specification for a set of SOAP headers to handle authentication, encryption, and signing directly in SOAP messages. Remoting servers that require secure hosting are best run in the ASP.NET process. Remoting servers that require additional speed but less security can be run in a Windows service host instead. You can configure security for serviced components by applying attributes within the code for the components and then specifying COM+ roles with the Component Services tool.
|
|