Is it possible to restrict who can use the public classes of an Assembly?

Asked

Viewed 44 times

4

The scenario is as follows:

I have a AssemblyProtegido.dll written in . NET that contains public classes. I wish only specific assemblies could consume such classes.

Assemplyprotegido.csproj

public class ClasseProtegida
{
    public void AlgumMetodo() {...}
}

Allowed.csproj

using AssemblyProtegido;

class Classe1 {
    //OK
    private ClasseProtegida obj = new ClasseProtegida();
}

Third.csproj

using AssemblyProtegido;

class Classe1 {
    //lançaria algum tipo de exceção ao tentar instanciar a classe.
    private ClasseProtegida obj = new ClasseProtegida();
}

This type of protection is possible?

Grateful.

1 answer

4


It is possible to make members internal visible to another specific Assembly using the attribute InternalsVisibleToAttribute. Obviously, you’d have to change public for internal the statements you want to share with other specific Assembly.

This is an Assembly level attribute.

Example of use (copy of MSDN website):

[assembly: InternalsVisibleTo("NomeDoAssemblyAmigo, PublicKey=002400000480000094" + 
                              "0000000602000000240000525341310004000" +
                              "001000100bf8c25fcd44838d87e245ab35bf7" +
                              "3ba2615707feea295709559b3de903fb95a93" +
                              "3d2729967c3184a97d7b84c7547cd87e435b5" +
                              "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
                              "712da72eec2533dc00f8529c3a0bbb4103282" +
                              "f0d894d5f34e9f0103c473dce9f4b457a5dee" +
                              "fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
                              "26e0b3")]

You can ignore the parameter PublicKey from what I remember, but that could be a security breach, because someone else can make an Assembly with the specified name and use the methods.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.