Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting .DLL access

Tags:

c#

Lets say I have two .dlls, Dll1 and Dll2.

DLL1 uses or makes calls into DLL2.

Is there a way I can ensure that it is DLL1 and only DLL1 who's making the calls into DLL2?

UPDATE 1

The reason behind this is:

I have a WinForms solution, and to keep it simple, it consists of a view project and a controllers project (which generates a seperate .dll). After installing the application on a client machine, I realise it is possible to view the application .dlls in it's "Program Files" folder. Somebody could potentially add a reference to the controller's .dll. I'd like to avoid this so that it's the view project and only the view project who's making the calls.

UPDATE 2
One of the reasons I like separating controllers into a separate project is that I could potentially have many different view projects calling into and using the same controllers. I then would only need to maintain one controller project for all views. For instance a win forms application and a test project or even a web site using the same controller project. But taking this approach , I would then be faced with the security problem I mention (avoiding and controlling improper use of my dll).

So I have one approach, compiling to one .dll, by using different folders, which I believe is correct and solves my security problem, but it conditions me to only having one view.

On the other hand if I have separate projects I am faced with the security issue.

I am still dubious as to how I should go about this as I would still like to continue using different projects for the reasons I mention.

UPDATE 3
Any suggestions on using the StrongNameIdentityPermission permission demand?
http://msdn.microsoft.com/en-us/library/ff648663.aspx (see: Restrict Which Code Can Call Your Code)
http://blogs.msmvps.com/manoj/2004/10/20/tip-strongnameidentitypermission/ http://www.morganskinner.com/Articles/StrongNameIdentityPermission/

Thanks

like image 275
Rauland Avatar asked Jan 18 '23 09:01

Rauland


2 Answers

You can make all types in DLL2 internal and use InternalsVisibleToAttribute in it set to DLL1.

To ensure that this will not be subverted, you should sign DLL1 and make sure you use its public key in the attribute.


Alternatively, as the author of both DLLs, consider combining the projects into one - set all the public methods that exist in DLL2 to internal, as before, but now only DLL1 exists and they can only be accessed by it.


Note: All the above assumes no reflection is used.

like image 114
Oded Avatar answered Jan 24 '23 13:01

Oded


You can using Reflection to check the current callstack. Said that I probably wouldn't do it as it's costly and slow.

You can get the stacktrace this way:

using System.Diagnostics;

// get call stack
StackTrace stackTrace = new StackTrace();

// get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
like image 23
Ignacio Soler Garcia Avatar answered Jan 24 '23 11:01

Ignacio Soler Garcia