Microsoft KB Archive/813830

= PRB: Appdomains Security Permission Error Message When You Access the Strong-Named Assembly from the \Bin Directory =

Article ID: 813830

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1

-





SYMPTOMS
In a Microsoft ASP.NET version 1.1 application, when you access a strong-named assembly that is stored in the \bin directory of your Web application that is not marked with AllowPartiallyTrustedCallersAttribute (APTCA) and when the trust level of the Web application is not set to Full Trust in the Web.config file, then you may receive the following error message instead of the expected SecurityException error message:

Assembly .dll security permission grant set is incompatible between appdomains

You will not receive the same error message in ASP.NET version 1.0, but the same underlying issue about strong-named assemblies also exists in ASP.NET 1.0.



CAUSE
Strong-named assemblies must be stored in the global assembly cache when they are used with ASP.NET Web applications. By default, strong-named assemblies will not grant access to partially trusted code, because of the visibility of strong-named assemblies in the global assembly cache. You can allow partially trusted code to call your strong-named assembly, but only after you explicitly set the AllowPartiallyTrustedCallersAttribute. If you do not explicitly declare the attribute, the caller requires Full Trust to use the code. Only apply the AllowPartiallyTrustedCallersAttribute to your strong-named assemblies after a careful assessment of the code.



RESOLUTION
Install strong-named assemblies in the global assembly cache for ASP.NET 1.0 and 1.1 Web applications. The following steps describe how to install a strong-named assembly in the global assembly cache, and explain how to use the AllowPartiallyTrustedCallersAttribute:

Install a Strong-Named Assembly in the Global Assembly Cache
You can use the global assembly cache to share assemblies across many applications. To install the strong-named assembly in the global assembly cache, follow these steps:
 * 1) To create a strong-name assembly, follow the steps in the &quot;More Information&quot; section of this article.
 * 2) Open Visual Studio .NET Command Prompt, and then type the following command: gacutil -I &quot;C:\[PathToBinDirectoryInVSProject]\sampledll.dll

Note: sampledll.dll is the name of the DLL that is signed with strong name.

Add the APTCA
Assemblies that are intended to be called by partially trusted code declare their intent by the assembly-level custom attribute AllowPartiallyTrustedCallers. To mark the strong-named assembly with APTCA, follow these steps:  To create a strong-name assembly, follow the steps in the &quot;More Information&quot; section of this article.  Replace the code in Class1 with the following code:

Visual C# .NET Code using System; using System.Reflection; using System.Runtime.InteropServices; using System.Security;

[assembly: AssemblyKeyFile(&quot;..\\..\\key.snk&quot;)] [assembly:AllowPartiallyTrustedCallers]

namespace SNAssemblyTest {   public class Class1 {  // Test Class } } Visual Basic .NET Code Imports System.Reflection Imports System.Runtime.InteropServices Imports System.Security

 

Public Class Class1 ' Test Class End Class 

Note Only apply the AllowPartiallyTrustedCallersAttribute after you have considered the security implications and have taken the necessary precautions. These precautions include a code review against the secure coding guidelines that are described in the &quot;Cause&quot; section of this article. Only apply this attribute to assemblies if the following criteria are met:
 * 1) The assemblies have been designed and been built with explicit attention to security considerations to help protect them against all callers, including potentially malicious callers.
 * 2) Appropriate security testing with partially trusted code is completed before the code is released.



STATUS
This behavior is by design.



Create a StrongName Assembly
 Start Microsoft Visual Studio .NET 2003.</li> On the File menu, point to New, and then click Project.</li> Select Visual C# project or Visual Basic Projects under Project Types.</li> Select Class Library under Templates.</li> Name the project as SNAssemblyTest .</li> Open the Visual Studio .NET Command Prompt, and then change the directory to the current project directory.</li> To create a strong-named key, type the following command: sn -k key.snk </li>  Replace the code in Class1 with the following code:

Visual C# .NET Code using System; using System.Reflection; using System.Runtime.InteropServices;

[assembly: AssemblyKeyFile(&quot;..\\..\\key.snk&quot;)]

namespace SNAssemblyTest {   public class Class1 {  // Test Class } } Visual Basic .NET Code Imports System.Reflection Imports System.Runtime.InteropServices

<Assembly: AssemblyKeyFile(&quot;..\..\key.snk&quot;)>

Public Class Class1 ' Test Class End Class </li> On the Build menu, click Build Solution.</li></ol>

Refer to the Assembly in an ASP.NET Web Application
 Start Visual Studio .NET, and then create a new ASP.NET Web Application project by using Visual C# .NET or Visual Basic .NET.</li> In Solution Explorer, right-click References, and then click Add Reference.</li> Click Browse, and then select the .dll file of the assembly that you created in the &quot;Create a StrongName Assembly&quot; section of this article (SNAssemblyTest.dll).</li> To open the Web.config file, double-click Web.config in the Solution Explorer.</li>  Add the following code trust level code in the <system.web> element of the Web.config file: <trust level=&quot;High&quot; /> </li> To run the project, click Start on the Debug menu. You may receive the error that is mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">