This documentation is archived and is not being maintained.

CodeConnectAccess Class

Specifies the network resource access that is granted to code.

Namespace:  System.Security.Policy
Assembly:  mscorlib (in mscorlib.dll)

public ref class CodeConnectAccess

This class is used in conjunction with the NetCodeGroup class to control how executing code can connect back to the site it was downloaded from. These classes provide programmatic control of the WebPermission granted to executing code that controls what schemes and ports the code can use access its origin site. You can specify scheme and port pairs that are permitted based on the scheme in the URL where the code originates.

To create a CodeConnectAccess object based on the code's origin scheme, use the CreateOriginSchemeAccess method.

To create a CodeConnectAccess object that can use any scheme but is restricted by port, use the CreateAnySchemeAccess method.

The following code example creates a NetCodeGroup object and adds it to the User policy level.

static void SetNetCodeGroupAccess()
    String^ userPolicyLevel = "User";
    // Locate the User policy level.
    PolicyLevel^ level = nullptr;
    System::Collections::IEnumerator^ ph = 
        level = (PolicyLevel^)ph->Current;
        if (level->Label == userPolicyLevel)
    if (level->Label != userPolicyLevel)
        throw gcnew ApplicationException("Could not find User policy level.");

    IMembershipCondition^ membership =
        gcnew UrlMembershipCondition("*");
    NetCodeGroup^ codeGroup = gcnew NetCodeGroup(membership);
    // Delete default settings.
    // Create an object that represents access to the FTP scheme and  
    // default port.
    CodeConnectAccess^ CodeAccessFtp = 
        gcnew CodeConnectAccess(Uri::UriSchemeFtp, 
    // Create an object that represents access to the HTTPS scheme  
    // and default port.
    CodeConnectAccess^ CodeAccessHttps = 
        gcnew CodeConnectAccess(Uri::UriSchemeHttps, 
    // Create an object that represents access to the origin  
    // scheme and port.
    CodeConnectAccess^ CodeAccessOrigin = 
    // Add connection access objects to the NetCodeGroup object.
    codeGroup->AddConnectAccess(Uri::UriSchemeHttp, CodeAccessFtp);
    codeGroup->AddConnectAccess(Uri::UriSchemeHttp, CodeAccessHttps);
    codeGroup->AddConnectAccess(Uri::UriSchemeHttp, CodeAccessOrigin);
    // Provide name and description information for caspol.exe tool.
    codeGroup->Name = "ContosoHttpCodeGroup";
    codeGroup->Description = "Code originating from can" +
        " connect back using the FTP or HTTPS.";
    // Add the code group to the User policy's root node.
    // Save the changes to the policy level.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0