Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Verification of Assembly Strong Names

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 13 Nov 2006 14:25 GMT
currently i have program that loads .net assemblies using the Assembly.Load
method from the Assembly class. the assemblies have several plug ins classes
that i load. I want to make sure that the assembly that i am loading is one
made by me. (have been signed by me using my StrongName Key). how do i do
that? Code examples will be great.
David Levine - 13 Nov 2006 16:46 GMT
Here's a routine that extracts the public key token from an assembly. You
can use this to extract the token from the assembly you loaded and then
compare that against the assembly that is executing, which is signed with
your company's strong name.

using System.Collections;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Reflection;

 /// <summary>
 /// This gets the public key portion of the strong name for the assembly.
 /// Use the reflection APIs to extract the strong name
 /// from the executing assembly.
 /// </summary>
 /// <returns></returns>
 private StrongNamePublicKeyBlob GetStrongNamePublicKey (Assembly asm)
 {
  // this gets the dynamic value
  IEnumerator e = asm.Evidence.GetEnumerator();
  while ( e.MoveNext() )
  {
   if ( e.Current.GetType() == typeof(
System.Security.Policy.StrongName ) )
   {
    StrongName u = (StrongName)e.Current;
    Trace.WriteLine( string.Format("Name= {0}, {1}; PublicKey= {2}",
u.Name, u.Version, u.PublicKey) );
    return u.PublicKey;
   }
  } //while ( e.MoveNext() )
  throw new SecurityException( "Unable to extract public key strong name
from assembly." );
 }//  GetStrongName

use it like this...

 public Assembly LoadMyPlugin (string pluginName )
 {
  Assembly a = Assembly.Load(pluginName); // or some variant of Loadxxx
  StrongNamePublicKeyBlob me = GetStrongNamePublicKey(
Assembly.GetExecutingAssembly() ); //
  StrongNamePublicKeyBlob caller = GetStrongNamePublicKey( a );
  if ( !me.Equals( caller ) )
   throw new InvalidOperationException( "You are not allowed to call
me." ); // or throw a SecurityException
 return a;  // I wrote it...
 }

> currently i have program that loads .net assemblies using the
> Assembly.Load
[quoted text clipped - 4 lines]
> made by me. (have been signed by me using my StrongName Key). how do i do
> that? Code examples will be great.
Dan - 13 Nov 2006 20:52 GMT
Wow that is what i am looking for. thank you very much.

> Here's a routine that extracts the public key token from an assembly. You
> can use this to extract the token from the assembly you loaded and then
[quoted text clipped - 54 lines]
> > made by me. (have been signed by me using my StrongName Key). how do i do
> > that? Code examples will be great.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.