Controlling Access to .NET Assemblies - c#

I have a group of dlls for WCF services. The contain contracts, interfaces etc. I would like to reuse this code in the API (client side) but don't want the consumers of the API to use these features.
Is there a way to say core.dll can only be used by clientcore.dll?

Well, you can give all the members of core.dll an access modifier of internal and then use InternalsVisibleToAttribute to give access to clientcore.dll.
To make life easier for your clients, you might actually want to merge core.dll into clientcore.dll using ILmerge. That way your callers don't need to copy a DLL they never use directly.

Yes, make your types internal and then use the friend assembly mechanism (via the InternalsVisibleToAttribute class).

sure, it's called:
Code Access Security

Related

.NET Application - How to hide implementation of dependent libraries?

This can be possible duplicate of this question, but I don't want to go with solution suggested i.e. use of Web Service.
Here is the scenario:
1) I want to expose one class library to clients. Let's name it "MyClassLibrary".
2) There are two more libraries "Library1" and "Library2" in the same solution for "MyClassLibrary" project.
3) "Libray1" is referred in "Library2" and "Library2" is referred in "MyClassLibrary".
4) There is no direct reference of "Libray1" inside "MyClassLibrary".
What do I want?
Client of "MyClassLibrary" should not be able to access classes, methods in "Library1". Is it ever possible? If I create nuget package for "MyClassLibrary", it will contain dll for "Library1" (as well as "Library2"). So using that dll, client can easily access stuff in "Library1" (as well as in "Library2"). How can I avoid that? I want my client to be able to access only required functions from "MyClassLibrary" and not implementation of "Library1" (and maybe "Library2"). How to achieve this?
If you want to make it less convenient for your client to access your code, you could use access specifiers to prevent him from doing so. For example you could use the InternalsVisibleTo attribute to hide your implementation, or put it all in one single assembly and make most of it private.
However, this is just to prevent him from accidentally using it.
If it contains secrets that you don't want him to know, you must not deliver it to him. One option would be to only deliver the interface of a webservice and have the actual service with your secrets run at your location. If you give him the assemblies, no mater how well protected, obfuscated or otherwise obscured, your secret is in the open.
Nope. Making things internal solves the problem you just described. As well, be aware of existence of [assembly:InternalsVisibleTo(...)].
Surely, rightly applied reflection makes even private things accessible. But I don't consider such a case.

Ensure only one class can access a reference dll?

I have a project where I want only one class to have access to a dll. I would be content with hiding the dll from intellisense, but still having access to it if that is an option. Either way, I want only one class to use the dll and for it not to be seen and/or accessible otherwise.
I'm using C# in visual studios.
Simply said: You can't do that (but keep reading).
Basically, a DLL (From the .NET perspective) is a bunch of code and config files. No more than that. So, given that you'll need to make public those classes in order to be used from another ones outside that assembly then you can not.
What you can do (I ended up doing this a few years ago) is to use some kind of reflection to determine which class (and namespace) is trying to access your assembly and only permit the proper one.
Other way is to implement a key negotiation between your DLL and the permitted assembly. You'll need to implement those manually, as far as I know.
Anyway, keep in mind there's always a way to bypass this kind of protection by disassembling and modifying your code. So, at least, don't forget to obfuscate the file. Anyway, this will just make it a little more painful, but still possible.
An alternate approach, if you goal is to stop people using the functionality of the dll by accident is to push your wrapper class into an intermediary assembly. Your project then references that intermediary project, rather than the underlying dll which effectively hides it. So your project structure would change to something like this:
Main Project -> references API Wrapper Project -> references API DLL
For this to work, your wrapper project needs to make sure that it doesn't accidentally expose any of the API DLL classes through its public interface.
Obviously this doesn't stop your developers from going in and adding a reference to the API DLL so that they can use the library directly, but if the goal is to stop accidental access to the API DLL classes because intellisense has helped the developer out a bit too much then it might be a viable option.

How do I expose a static class in a plugin without distributing the assembly and full MSIL for that class?

I have a software in c# that exposes an interface for plugins. The plugins come with the ability to interact with a factory that creates certain tools for use in the plugin, however I do not want to distribute the code for the factory compiled along with the assembly for the plugin interface. The problem is that the factory is not an interface, its a static factory interior to the system. How do I expose what the factory does, without exposing what the factory is, so to speak.
Thank you for your help.
Since the factory is a dependency of any plugins, you cannot obfuscate/encrypt it - plugins would not be able to use the factory if you did. You won't have to distribute the C# source to your factory, but you will have to include the compiled assembly with the factory, or the plugins will not work. So the short answer is "You can't".
You could use ILMerge to combine the assembly/assemblies for the factory with those for your software. Other than that... you are going to need to put the code for the factory somewhere, and if it is .NET that means distributing assemblies.
One very unappealing option is to include the code to your static factory encrypted as a resource to another assembly, have the another assembly decrypt and compile it dynamically and then only ever use reflection to access it where necessary. Of course this is a possibility, though I would rarely recommend it.
A better option if you have to share your code is to actually share the code.
You could wrap it inside a class that implements IFactoryThing, this way, client code would do a call on this interface instead.
Still you would have to distribute your code.

In C#, how to restrict who can call a method at compile time

In C#, is it possible to restrict who can call a method at compile time?
I've looked into directives, but that didn't work since I can't assign values to symbols.
#define WHO VisualStudioUser.Current // does not work
I also looked into Code Access Security (CAS) but that's runtime enforcement, not compile time.
The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly.
here's more details...
I'm building a framework or a series or assemblies for a team of developers. Because of our software license restrictions, I can only allow a few developers to write code to make a call to some restricted methods. The developers will not have access to the source code of the framework but they'll have access to the compiled framework assemblies.
The quick answer will be: No this isn't possible, and if you need to do it, you're Doing It Wrong.
How would this even work? Does it depend who who's running the code or who wrote it?
Edit There's kind of a way using InternalsVisibleTo and restricting accessing in source control to the assemblies that InternalsVisibleTo is specified for. See Jordão's answer
The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly.
One way is to mark the method private or internal, it won't be callable by anyone outside the assembly. UPDATE: Also take a look at the InternalsVisibleTo attribute, which is used to define which assemblies can "see" internals of your assembly.
Another way is to divide the code you want to distribute from the code you don't want people to call into separate assemblies. Maybe you just share an assembly mostly of interfaces with your users, that they them compile against; and you have a separate assembly with implementations that they shouldn't reference directly. Your internal team would have access to the implementation assembly. This is just a common form of dependency management, the dependency inversion principle.
Draft:
Compile the restricted code into (obfuscated) DLLs: TypeA.dll, TypeB.dll etc.
Define an interface for each type, and compile them into separate DLLs: ITypeA.dll, ITypeB.dll etc.
Create a "guard assembly", and embed all restricted assemblies into it: Guard.dll. This has a ResolveEventHandler, and methods to instantiate different types defined in the embedded restricted DLLs. Instances are returned through their interface.
Developers get the interface DLLs and the Guard.dll. Each developer can get a Guard.dll with special authentication tokens in it. For example, a Guard.dll can be bound to PC, an IP address, a GUID issued to the developer, anything.
The developer can instantiate those types for which she has the proper authentication code, and uses the object instance through an interface.
Sorry this is a bit fuzzy, because it was more than a year ago when I used these techniques. I hope the main idea is clear.
Can you try using Extensible C# developed by ResolveCorp, some of the links for study and implementation are:
http://zef.me/782/extensible-c
http://www.codeproject.com/KB/architecture/DbCwithXCSharp.aspx
http://weblogs.asp.net/nunitaddin/archive/2003/02/14/2412.aspx
http://www.devx.com/dotnet/Article/11579/0/page/5

Generate proxy classes not used by WebMethod

I have sortof the opposite of this question:
wsdl : Generate Proxy for the WebMethods but not the other dependent classes
How can one auto-generate other classes (utility classes) that are useful on the client side but are neither DataContracts nor ServiceContracts? In other words, wanting to extract specific classes instead of including entire DLL's.
Edit: Yes arbitrary classes. I think we will end up extracting those to a DLL other then the ones they're currently part of. Just wondering if there is a way using reflection or tool to copy out only specific classes from a source DLL to a destination DLL. "Proxy" is probably the wrong word because the methods wouldn't call WCF. Instead they would be normal classes, other than that they were copied from a source DLL. (The reason is, not wanting to share all of (decompilable) DLL's.)
If the source dll is something you control, then copying classes is really going to lead to problems down the road. The better approach would be to extract the shared classes to a "Shared" or "Interop" or "Common" dll that the client and server projects can both reference.
Doing this also helps separate data from logic since the shared/interop/common project shouldn't reference anything else and is very simply data containers.
You can't specify method implementaion thru WSDL. In order to accomplish what you are trying to do you would need to create a build script / marcro that creates and compiles a client library which has the proxy and your util methods.
HTH

Categories