How to Find the PublicKeyToken for a .NET Assembly: Methods and Best Practices

Dec 01, 2025 · Programming · 28 views · 7.8

Keywords: .NET | Assembly | PublicKeyToken

Abstract: This article provides an in-depth exploration of various methods for finding the PublicKeyToken of a .NET assembly, with a focus on using PowerShell reflection as the best practice. It begins by explaining the critical role of PublicKeyToken in assembly identification, then demonstrates step-by-step how to retrieve the full assembly name, including version, culture, and public key token, via PowerShell commands. As supplementary approaches, it briefly covers alternative tools such as sn.exe and Reflector. Through practical code examples and detailed analysis, this paper aims to assist developers in accurately configuring files like web.config, preventing runtime issues caused by incorrect public key tokens.

Introduction

In .NET development, assembly identification typically relies on four key attributes: name, version, culture, and PublicKeyToken. PublicKeyToken, as part of a strong-named assembly, ensures uniqueness and integrity. In configuration files, such as web.config, correctly specifying the PublicKeyToken is crucial for loading specific assemblies. When misconfigured, common runtime errors include "cannot load assembly" or "type not found," often due to PublicKeyToken mismatches.

Core Role of PublicKeyToken

PublicKeyToken is a hash value derived from a public key, used to uniquely identify an assembly in the Global Assembly Cache (GAC) or within applications. In the .NET framework, when referencing external assemblies, the system validates the PublicKeyToken to ensure the assembly has not been tampered with. For example, when configuring a membership provider, the type attribute must include full assembly information, including the PublicKeyToken. If the token is incorrect, the .NET runtime fails to resolve the type, leading to load failures.

Finding PublicKeyToken Using PowerShell

Based on best practices, PowerShell offers an efficient and direct method to retrieve the PublicKeyToken. Through .NET reflection, assemblies can be dynamically loaded and their full names retrieved. Here are the detailed steps:

First, open the PowerShell command-line interface. Ensure you have appropriate permissions to access the target DLL file. Execute the following command, replacing the path with the actual location of your DLL:

([system.reflection.assembly]::loadfile("C:\path\to\MyDLL.dll")).FullName

This command uses the LoadFile method of the System.Reflection.Assembly class to load the specified assembly, then retrieves the full name via the FullName property. The output format is typically:

MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a

From the output, you can extract the PublicKeyToken (e.g., 669e0ddf0bb1aa2a) and use it in configuration files. This method is advantageous as it requires no additional tools, leverages .NET framework capabilities directly, and works in various environments.

Alternative Methods as Supplements

Beyond PowerShell, other tools can be used to find the PublicKeyToken. For instance, using sn.exe (Strong Name Tool), which is part of the .NET SDK. The command is:

sn -T YourAssembly.dll

This command outputs the public key token but requires .NET SDK or Visual Studio installation. Another approach is to use decompilation tools like Reflector, loading the assembly and inspecting its properties. These methods serve as alternatives, but PowerShell is preferred due to its convenience and no need for extra installations.

Practical Applications and Considerations

When configuring providers in web.config, ensure the type attribute includes the correct PublicKeyToken. For example, for SqlMembershipProvider, the configuration should resemble:

<add name="AspNetSqlMemProvider" type="System.Web.Security.SqlMembershipProvider, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

If the PublicKeyToken is wrong, it may cause runtime exceptions. It is recommended to verify all assembly references before deployment, using the methods described to check tokens. Additionally, for non-strong-named assemblies, the PublicKeyToken might be null, which should be noted in configurations.

Conclusion

Finding the PublicKeyToken is a fundamental skill in .NET development, especially for configuration management and assembly referencing. By using the PowerShell reflection method, developers can quickly and accurately obtain tokens, avoiding common configuration errors. Combining other tools as supplements ensures efficiency in various scenarios. Proper use of PublicKeyToken not only enhances application stability but also improves security by preventing unauthorized assembly loads.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.