CodeConnectAccess Class

CodeConnectAccess Class

 

Specifies the network resource access that is granted to code.

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

System.Object
  System.Security.Policy.CodeConnectAccess

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class CodeConnectAccess

NameDescription
System_CAPS_pubmethodCodeConnectAccess(String, Int32)

Initializes a new instance of the CodeConnectAccess class.

NameDescription
System_CAPS_pubpropertyPort

Gets the port represented by the current instance.

System_CAPS_pubpropertyScheme

Gets the URI scheme represented by the current instance.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateAnySchemeAccess(Int32)

Returns a CodeConnectAccess instance that represents access to the specified port using any scheme.

System_CAPS_pubmethodSystem_CAPS_staticCreateOriginSchemeAccess(Int32)

Returns a CodeConnectAccess instance that represents access to the specified port using the code's scheme of origin.

System_CAPS_pubmethodEquals(Object)

Returns a value indicating whether two CodeConnectAccess objects represent the same scheme and port.(Overrides Object.Equals(Object).)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as a hash function for a particular type.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticAnyScheme

Contains the string value that represents the scheme wildcard.

System_CAPS_pubfieldSystem_CAPS_staticDefaultPort

Contains the value used to represent the default port.

System_CAPS_pubfieldSystem_CAPS_staticOriginPort

Contains the value used to represent the port value in the URI where code originated.

System_CAPS_pubfieldSystem_CAPS_staticOriginScheme

Contains the value used to represent the scheme in the URL where the code originated.

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.

public static void SetNetCodeGroupAccess()
{
    const string userPolicyLevel = "User";
    // Locate the User policy level.
    PolicyLevel level = null;
    System.Collections.IEnumerator ph = 
        System.Security.SecurityManager.PolicyHierarchy();
    while(ph.MoveNext())
    {
        level = (PolicyLevel)ph.Current;
        if( level.Label == userPolicyLevel )
        {
            break;
        }
    }
    if (level.Label != userPolicyLevel)
        throw new ApplicationException("Could not find User policy level.");

    IMembershipCondition membership =
        new UrlMembershipCondition(@"http://www.contoso.com/*");
    NetCodeGroup codeGroup = new NetCodeGroup(membership);
    // Delete default settings.
    codeGroup.ResetConnectAccess();
    // Create an object that represents access to the FTP scheme and default port.
    CodeConnectAccess a1 = new CodeConnectAccess(Uri.UriSchemeFtp, CodeConnectAccess.DefaultPort);
    // Create an object that represents access to the HTTPS scheme and default port.
    CodeConnectAccess a2 = new CodeConnectAccess(Uri.UriSchemeHttps, CodeConnectAccess.DefaultPort);
    // Create an object that represents access to the origin scheme and port.
    CodeConnectAccess a3 = CodeConnectAccess.CreateOriginSchemeAccess(CodeConnectAccess.OriginPort);
    // Add connection access objects to the NetCodeGroup object.
    codeGroup.AddConnectAccess(Uri.UriSchemeHttp, a1);
    codeGroup.AddConnectAccess(Uri.UriSchemeHttp, a2);
    codeGroup.AddConnectAccess(Uri.UriSchemeHttp, a3);
    // Provide name and description information for caspol.exe tool.
    codeGroup.Name = "ContosoHttpCodeGroup";
    codeGroup.Description = "Code originating from contoso.com can connect back using the FTP or HTTPS.";
    // Add the code group to the User policy's root node.
    level.RootCodeGroup.AddChild(codeGroup);
    // Save the changes to the policy level.
    System.Security.SecurityManager.SavePolicy();
}

.NET Framework
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft