Skip to content

Latest commit

 

History

History
98 lines (74 loc) · 5.42 KB

File metadata and controls

98 lines (74 loc) · 5.42 KB
title How to: Create Signed Friend Assemblies (Visual Basic)
ms.custom
ms.date 03/14/2018
ms.prod .net
ms.suite
ms.technology
devlang-visual-basic
ms.tgt_pltfrm
ms.topic article
ms.assetid f2afd83d-b044-484b-a56d-56d0a8a40647
author rpetrusha
ms.author ronpet

How to: Create Signed Friend Assemblies (Visual Basic)

This example shows how to use friend assemblies with assemblies that have strong names. Both assemblies must be strong named. Although both assemblies in this example use the same keys, you could use different keys for two assemblies.

To create a signed assembly and a friend assembly

  1. Open a command prompt.

  2. Use the following sequence of commands with the Strong Name tool to generate a keyfile and to display its public key. For more information, see [Sn.exe (Strong Name Tool)]Sn.exe (Strong Name Tool)).

    1. Generate a strong-name key for this example and store it in the file FriendAssemblies.snk:

      sn -k FriendAssemblies.snk

    2. Extract the public key from FriendAssemblies.snk and put it into FriendAssemblies.publickey:

      sn -p FriendAssemblies.snk FriendAssemblies.publickey

    3. Display the public key stored in the file FriendAssemblies.publickey:

      sn -tp FriendAssemblies.publickey

  3. Create a Visual Basic file named friend_signed_A that contains the following code. The code uses the xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute to declare friend_signed_B as a friend assembly.

    The Strong Name tool generates a new public key every time it runs. Therefore, you must replace the public key in the following code with the public key you just generated, as shown in the following example.

    ' friend_signed_A.vb  
    ' Compile with:   
    ' Vbc -target:library -keyfile:FriendAssemblies.snk friend_signed_A.vb  
    Imports System.Runtime.CompilerServices  
    
    <Assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")>   
    Public Class Class1  
        Public Sub Test()  
            System.Console.WriteLine("Class1.Test")  
            System.Console.ReadLine()  
        End Sub  
    End Class  
  4. Compile and sign friend_signed_A by using the following command.

    Vbc -target:library -keyfile:FriendAssemblies.snk friend_signed_A.vb  
  5. Create a Visual Basic file that is named friend_signed_B and contains the following code. Because friend_signed_A specifies friend_signed_B as a friend assembly, the code in friend_signed_B can access Friend types and members from friend_signed_A. The file contains the following code.

    ' friend_signed_B.vb  
    ' Compile with:   
    ' Vbc -keyfile:FriendAssemblies.snk -r:friend_signed_A.dll friend_signed_B.vb  
    Module Sample  
        Public Sub Main()  
            Dim inst As New Class1  
            inst.Test()  
        End Sub  
    End Module  
  6. Compile and sign friend_signed_B by using the following command.

    vbc -keyfile:FriendAssemblies.snk -r:friend_signed_A.dll friend_signed_B.vb  

    The name of the assembly generated by the compiler must match the friend assembly name passed to the xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute. You can explicitly set the assembly by using the -out compiler option. For more information, see -out (Visual Basic).

  7. Run the friend_signed_B.exe file.

    The program displays the string "Class1.Test".

.NET Framework Security

There are similarities between the xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute and the xref:System.Security.Permissions.StrongNameIdentityPermission class. The main difference is that xref:System.Security.Permissions.StrongNameIdentityPermission can demand security permissions to run a particular section of code, whereas the xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute controls the visibility of Friend types and members.

See Also

xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute
Assemblies and the Global Assembly Cache (Visual Basic)
Friend Assemblies (Visual Basic)
How to: Create Unsigned Friend Assemblies (Visual Basic)
-keyfile
[Sn.exe (Strong Name Tool)]Sn.exe (Strong Name Tool))
Creating and Using Strong-Named Assemblies
Programming Concepts