Microsoft KB Archive/815708

= How to check security in a COM+ application by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815708

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 309023.



For a Microsoft Visual C# .NET version of this article, see 815709.



SUMMARY
An important function of most COM+ applications is to provide enhanced security. By using the System.EnterpriseServices namespace in the Microsoft .NET Framework, you can test and retrieve information that is based on COM+ security in the .NET Framework application.

To initiate COM+ security in a Microsoft Visual C++ .NET or Microsoft Visual C++ 2005 application, various class-level attributes and assembly-level attributes are used. Additionally, some objects are provided by the .NET Framework, such as the System.EnterpriseServices.SecurityCallContext object.

IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Create a new Visual C++ .NET class library
 * Create class-level attributes and assembly-level attributes
 * Create the component code
 * Build the project, and then install the application
 * Create the test harness project
 * Run the test harness project
 * Add a user to the manager role, and then retest the test harness project
 * Troubleshooting
 * REFERENCES



INTRODUCTION
This step-by-step article discusses how to check the security of a COM+ application by using Microsoft Visual C++ .NET or Visual C++ 2005. In the .NET Framework, you can retrieve COM+ security information by using the System.EnterpriseServices namespace.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Developing COM+ applications
 * Developing classes by using Visual C++ .NET or Visual C++ 2005
 * Declaring class-level attributes and assembly-level attributes

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual C++ .NET
 * Visual C++ 2005

back to the top

Create a new Visual C++ .NET or Visual C++ 2005 class library
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates.

In Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Class Library (.NET) under Templates.

In Visual C++ 2005, click Visual C++ under Project Types, and then click Class Library under Templates. In the Name text box, type CheckSecurity . In the Location text box, type C:\Test, and then click OK.</li>  In Visual C++ .NET 2002, add the following code to the CheckSecurity.h file: In Visual C++ .NET 2003, click Add Reference on the Project menu. In the .NET components list, select System.EnterpriseServices, and then click OK.
 * 1) using &quot;System.EnterpriseServices.dll&quot;

In Visual C++ 2005, click Reference on the Project menu, and then click Add New Reference. In the .NET components list, select System.EnterpriseServices, and then click OK. </li> To create a strong name for your class library, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Start, point to Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.

Note In Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.</li> Locate your Project directory.</li>  Type the following command at the command prompt: sn.exe -k VCSecurity.SNK </li></ol> </li>  In Solution Explorer, double-click the AssemblyInfo.cpp file, and then locate the following sample code: [assembly:AssemblyKeyFileAttribute(&quot;&quot;)]; Replace the previous sample code with the following sample code: [assembly:AssemblyKeyFileAttribute(&quot;VCSecurity.SNK&quot;)]; </li></ol>

back to the top

Create class-level attributes and assembly-level attributes
<ol> In Solution Explorer, double-click the CheckSecurity.h file.</li>  Add the following code to the CheckSecurity.h file after the using namespace System; line: using namespace System::EnterpriseServices; Note Use the using namespace statement in the System.EnterpriseServices namespaces so that you do not have to qualify declarations from these namespaces later in your code. </li>  Add the following Assembly attribute after the using statement to turn on security checking at the application level: [assembly:ApplicationAccessControl(true)]; </li>  Add the following attribute after the Assembly attribute that you added in step 3 to set the application as a server-activated application: [assembly:ApplicationActivation(ActivationOption::Server)]; </li>  Locate the following code: public __gc class Class1 </li>  Add the following class attribute to turn on security checking in the component: [ComponentAccessControl(true)] </li>  Add a second class attribute to create a Manager role with no default users: [SecurityRole(&quot;Manager&quot;)] </li>  Add a third class attribute to create a Guest role that includes the Everyone user group: [SecurityRole(&quot;Guest&quot;,true)] </li></ol>

back to the top

Create the component code
<ol> Rename the Class1 class as the VCSecure class in the class definition.</li>  Modify the code as follows to inherit the VCSecure class from the System.EnterpriseServices.ServicedComponent class: public __gc class VCSecure : public System::EnterpriseServices::ServicedComponent </li> <li> Add the following code to the class: public: bool CheckManagerRole(void) {   if(ContextUtil::IsSecurityEnabled) {       return SecurityCallContext::CurrentCall->IsCallerInRole(&quot;Manager&quot;); }

return false; }

String* GetAccountName(void) {   if(ContextUtil::IsSecurityEnabled) {       return SecurityCallContext::CurrentCall->OriginalCaller->AccountName; }

return NULL; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article. </li> <li> Add the following header file declaration after the #pragma once statement:
 * 1) include <tchar.h> </li></ol>

back to the top

Build the project, and then install the application

 * 1) Save the project, and then build the project.
 * 2) Click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
 * 3) At the command prompt, locate the Debug folder for your project.
 * 4) Use the following command to install the assembly in the Global Assembly Cache:

gacutil /i CheckSecurity.dll

Note You can also do this by using the .NET Configuration snap-in for the Microsoft Management Console.
 * 1) Use the following command to register the application with COM+:

regsvcs CheckSecurity.dll

Note Administrative credentials are required for this step.

back to the top

Create the test harness project
<ol> <li>Start Visual Studio .NET or Visual Studio 2005.</li> <li>On the File menu, point to New, and then click Project.</li> <li>In Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual C++ 2005, click Visual C++ under Project Types, and then click Console Application under Templates.</li> <li>In the Name text box, type TestClient .</li> <li>In the Location text box, type C:\Test, and then click OK.</li> <li> In Visual C++ .NET 2002, add the following code to the TestClient.cpp file:
 * 1) using &quot;System.EnterpriseServices.dll&quot;

In Visual C++ .NET 2003, click Add Reference on the Project menu. In the .NET components list, select System.EnterpriseServices, and then click Select. Click Browse, locate the bin folder of the CheckSecurity project, click CheckSecurity.dll, and then click Open. Click OK to close the Open dialog box.
 * 1) using &quot;..\CheckSecurity\Debug\CheckSecurity.dll&quot;

In Visual C++ 2005, click Reference on the Project menu, and then click Add New Reference. In the .NET components list, click System.EnterpriseServices, and then click Select. Click Browse, locate the bin folder of the CheckSecurity project, click CheckSecurity.dll, and then click Open. Click OK to close the Open dialog box. </li> <li>In Solution Explorer, double-click the TestClient.cpp file.

The TestClient.cpp file opens.</li> <li> In the Main function, replace the existing code with the following code: CheckSecurity::VCSecure *secObject = new CheckSecurity::VCSecure;

if(secObject->CheckManagerRole) {          Console::WriteLine(S&quot;You are a manager.&quot;); } else {   Console::WriteLine(S&quot;You are not a manager.&quot;); }

Console::WriteLine(S&quot;Your account name is: {0}&quot;, secObject->GetAccountName); secObject->Dispose;

Console::WriteLine(S&quot;Press ENTER to exit&quot;); Console::ReadLine;

return 0; </li></ol>

back to the top

Run the test harness project
<ol> <li>Save and then build the test harness project.</li> <li>Run the project, and then confirm that the

&quot;You are not a manager&quot;

message now appears with your Windows user information.

Confirm this message before you quit the project.</li></ol>

back to the top

Add a user to the manager role, and then retest the test harness project
<ol> <li>Click Start, point to Programs, point to Administrative Tools, and then click Component Services.</li> <li>In the Component Services administration tool, locate the ComponentServices\Computers\My Computer\COM+ Applications\CheckSecurity application.</li> <li>Expand the Roles folder. Expand the Manager folder. Expand the Users folder, and then right-click Users.</li> <li>Click New, and then click User. Click Advanced, and then click Find Now. In the list of users, click the account that was displayed by the security application that you previously tested.

This will be your user account.</li> <li>Click OK, and then click OK again to close the Select Users or Groups dialog box.</li> <li>Retest the test harness project to confirm that the

&quot;You are a manager&quot;

message now appears.</li></ol>

back to the top

Troubleshooting
The following information may help you with troubleshooting your project:
 * The client code for this example works when the client application is installed on the same computer as the server component. You must use the .NET Framework remoting feature if you want to install the client application on a different computer.
 * Use the uninstall option for Gactutil.exe (gacutil /u server) to remove the server component from the Global Assembly Cache. When you install a new version, the earlier version is not removed from the cache.

back to the top

<div class="references_section">