StrongNameCodeGroup Class

Inheritance: System.Object
System.Security.Policy
StrongNameCodeGroup

Defines a code group that is identified by the strong name of a code assembly. Code groups are the fundamental building blocks of a code access security (CAS) policy.

Syntax


public sealed class StrongNameCodeGroup : CodeGroup
                

Remarks

A StrongNameCodeGroup contains conditions based on the strong name of an assembly. This strong name is represented by the publisher's public key, the strong name of the assembly, or both. The StrongNameIdentityCondition class is used to represent these conditions.

When an assembly is loaded, the common language runtime (CLR) evaluates the code access security policy by traversing the hierarchy of code groups. For each code group, the CLR checks if the assembly's evidence matches the membership condition of the code group. If a match is found, the permissions associated with that code group are granted to the assembly.

Methods

Equals

Determines whether the specified object is equal to the current object.


public override bool Equals(
    object obj
)
                

Parameters

Name Type Description
obj object The object to compare with the current object.

GetHashCode

Serves as the default hash function.


public override int GetHashCode()
                

Resolve

Resolves the code group based on the specified evidence.


public override PolicyStatement Resolve(
    Evidence evidence
)
                

Parameters

Name Type Description
evidence Evidence The evidence to resolve the code group with.

Returns

A PolicyStatement object that represents the resolved permissions for the assembly, or null if the assembly does not belong to this code group.

Constructors

StrongNameCodeGroup Constructor

Initializes a new instance of the StrongNameCodeGroup class.


public StrongNameCodeGroup(
    IIdentityPermissionFactory identityFactory,
    StrongNamePublicKeyBlob publicKeyBlob,
    string description,
    string hashBase,
    string hashLocation
)
                

Parameters

Name Type Description
identityFactory IIdentityPermissionFactory An object that is used to create identity permissions.
publicKeyBlob StrongNamePublicKeyBlob A StrongNamePublicKeyBlob object representing the public key of the assembly.
description string A description of the code group.
hashBase string The hash value of the assembly.
hashLocation string The location of the assembly.

Example

C# Example

The following example shows how to create a StrongNameCodeGroup with specific parameters and add it to a policy.


using System;
using System.Security.Policy;
using System.Security.Cryptography;
using System.IO;

// Assume you have a public key blob and other necessary info
// For demonstration purposes, we'll use placeholders.
// In a real scenario, you would obtain these from an assembly.

// Example public key blob (replace with actual)
byte[] publicKeyBytes = { /* your public key bytes */ };
StrongNamePublicKeyBlob publicKeyBlob = new StrongNamePublicKeyBlob(publicKeyBytes);

// Example description, hash base, and hash location
string description = "Code group for authenticated strong-named assemblies.";
string hashBase = "MyAssembly.dll";
string hashLocation = "http://example.com/assemblies/";

// Create a new StrongNameCodeGroup
StrongNameCodeGroup strongNameGroup = new StrongNameCodeGroup(
    new UrlIdentityPermission(new Url("http://example.com/*")), // Example identity factory
    publicKeyBlob,
    description,
    hashBase,
    hashLocation
);

// In a real policy management scenario, you would add this group
// to a PolicyLevel. For example:
//
// PolicyLevel applicationLevel = AppDomain.CurrentDomain.Evidence.GetPolicyLevel();
// applicationLevel.RootCodeGroup.AddChild(strongNameGroup);
//
// Console.WriteLine($"StrongNameCodeGroup created for assembly: {hashBase}");
                    

See Also