Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Getting reference of calling Assembly.

Getting reference of calling Assembly.

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Syed Muhammad Kamran
    wrote on last edited by
    #1

    How can I get a reference of the calling Assembly at Runtime? I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly. For better understanding, this is what I am trying to do. I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.

    /**
    * Constructor of public sealed class RestrictedType1
    * Cotains in RestrictedAssembly.
    * Caller is required to pass a type object
    * o is the Type who wish to instantiate this class.
    */
    public RestrictedType1(Type o) {
    Type t = this.GetType ();
    byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
    byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
    AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");

    if(t1Token != null && t2Token != null) {
    if(t1Token.Length == t2Token.Length) {
    for(int i = 0; i< t1Token.Length; i++) {
    if (t1Token[i] == t2Token[i])
    continue;
    else
    throw ex;
    }
    // If get here successfullyDone;
    ex = null;
    } else
    throw ex;
    } else
    throw ex;
    }

    I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception. Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.

    RestrictedType1 res = new RestrictedType1(this.GetType);

    However, suppose if some one initiate a call to RestrictedType1 constructor

    RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));

    This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a refere

    G 1 Reply Last reply
    0
    • S Syed Muhammad Kamran

      How can I get a reference of the calling Assembly at Runtime? I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly. For better understanding, this is what I am trying to do. I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.

      /**
      * Constructor of public sealed class RestrictedType1
      * Cotains in RestrictedAssembly.
      * Caller is required to pass a type object
      * o is the Type who wish to instantiate this class.
      */
      public RestrictedType1(Type o) {
      Type t = this.GetType ();
      byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
      byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
      AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");

      if(t1Token != null && t2Token != null) {
      if(t1Token.Length == t2Token.Length) {
      for(int i = 0; i< t1Token.Length; i++) {
      if (t1Token[i] == t2Token[i])
      continue;
      else
      throw ex;
      }
      // If get here successfullyDone;
      ex = null;
      } else
      throw ex;
      } else
      throw ex;
      }

      I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception. Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.

      RestrictedType1 res = new RestrictedType1(this.GetType);

      However, suppose if some one initiate a call to RestrictedType1 constructor

      RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));

      This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a refere

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Please don't cross post.

      --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups