StrongNameIdentityCondition Class

System.Net.Security

public sealed class StrongNameIdentityCondition : IRightEnvironmentCondition

Represents a strong name identity condition for code access security.

This class is used to specify a condition based on the strong name of an assembly. It allows you to grant or deny permissions to code based on its strong name, including its public key token, name, and version.

Remarks

Strong name signing is a process that uses public-key cryptography to provide a unique identity to an assembly. This identity is independent of the name of the file containing the manifest. A strong name consists of the assembly's simple name, version number, resource information, and the public key or hash of the public key that generated the digital signature.

The StrongNameIdentityCondition class is part of the .NET Framework's code access security (CAS) model. While CAS has been largely deprecated in favor of other security mechanisms, understanding classes like StrongNameIdentityCondition can still be valuable for working with older .NET applications or for comprehending security concepts.

Syntax

public sealed class StrongNameIdentityCondition : IRightEnvironmentCondition

Constructors

The StrongNameIdentityCondition class has the following public constructors:

Constructor Description
public StrongNameIdentityCondition(StrongNamePublicKeyBlob publicKeyBlob, string name, Version version)
Initializes a new instance of the StrongNameIdentityCondition class with the specified public key, name, and version.
public StrongNameIdentityCondition(string blobFileName)
Initializes a new instance of the StrongNameIdentityCondition class using the public key blob from the specified file.

Methods

The StrongNameIdentityCondition class inherits the following methods from its base classes and implements the following methods:

Method Description
public override bool Equals(object o)
Determines whether the specified object is equal to the current object.
public bool Equals(StrongNameIdentityCondition value)
Determines whether the specified StrongNameIdentityCondition object is equal to the current object.
public override int GetHashCode()
Serves as a hash function for the type.
public virtual string ToString()
Returns a string that represents the current object.
public bool Check(object context)
When implemented in a derived class, checks if the specified security context satisfies the condition.

Properties

The StrongNameIdentityCondition class has the following public properties:

Property Type Description
StrongNamePublicKeyBlob PublicKeyBlob { get; }
StrongNamePublicKeyBlob Gets the public key blob associated with the strong name.
string Name { get; }
string Gets the name of the strong name.
Version Version { get; }
Version Gets the version of the strong name.

Examples

Here's an example of how to use the StrongNameIdentityCondition to create a security condition:

// Assume you have a StrongNamePublicKeyBlob, name, and version using System.Net.Security; using System.Security.Policy; using System.Reflection; // Load assembly to get public key blob (example) Assembly assembly = typeof(StrongNameIdentityCondition).Assembly; StrongNameKey snKey = new StrongNameKey("path_to_your_key.snk"); StrongNamePublicKeyBlob publicKeyBlob = snKey.PublicKey; string assemblyName = assembly.GetName().Name; Version assemblyVersion = assembly.GetName().Version; // Create the strong name identity condition StrongNameIdentityCondition condition = new StrongNameIdentityCondition(publicKeyBlob, assemblyName, assemblyVersion); // You can then use this condition with a CodeGroup to grant permissions PolicyStatement ps = new PolicyStatement(new PermissionSet(PermissionState.Unrestricted), PolicyStatementAttribute.Nothing); StrongNameMembershipCondition membership = new StrongNameMembershipCondition(publicKeyBlob, assemblyName, assemblyVersion); CodeGroup rootCodeGroup = new NetCodeGroup(new AllMembershipCondition()); rootCodeGroup.AddChild(new NamedPermissionSet("FullTrust", PermissionSet.Unrestricted)); rootCodeGroup.AddChild(new CodeGroup(membership, ps)); // Note: CAS is largely deprecated, this is for illustrative purposes.
Back to top