A .NET assembly is “signed” if the developer compiled the assembly with the private key of a digital signature. When the system later loads the assembly, it verifies the assembly with the corresponding public key. Occasionally you may need to determine whether an assembly you have loaded has been signed.
If the .NET application or library you are building is signed, then all assemblies referenced by your application at compile time must also be signed. If you add to your signed application a reference to an unsigned assembly, you will receive this error when you compile your application:
Error 1 Assembly generation failed — Referenced assembly ‘XYZ’ does not have a strong name
However, if the application you are building is unsigned, or if you are loading an assembly at run-time, then it’s possible to load an unsigned assembly.
The following code demonstrates how to determine if a loaded .NET assembly is signed. In this example, “MyType” represents the name of any class defined in the assembly that you want to check:
Assembly asm = Assembly.GetAssembly( typeof( MyType ) ); if (asm != null) { AssemblyName asmName = asm.GetName(); byte[] key = asmName.GetPublicKey(); bool isSignedAsm = key.Length > 0; Console.WriteLine( "IsSignedAssembly={0}", isSignedAsm ); }
See .NET Assembly FAQ – Part 3 – Strong Names and Signing for more information about signed assemblies.
how can i know if an assembly has a digital signature
tab in the properties i tried to use FileVersionInfo,
In Visual Studio:
1. Select the project.
2. Click menu item “Project > Properties”.
3. Click the “Signing” tab.
4. If the “Sign the assembly” checkbox is checked, then the assembly has a digital signature.
thank you man sorry i didnot clarify my question i want to know how to do that programmatically
(ie get dll path and know if it has digital signature tab , rightclick–> properties–>digital signature)
The C# code to determine if an assembly has a digital signature is in the article above.
i dont know what does “typeof( MyType )” mean
could you plz give me an example
and thank you again 🙂
i want to get the path for the dll and then check if it has digital signature then get the signer name and the time stamp value,
bool sigend( string dll_path)
{
}
MyType is any type that’s defined in the assembly you are trying to check for digital signature.
So if your assembly has defined a Person class:
public class Person { … }
Then using the example in the article, you would access the assembly by:
Assembly asm = Assembly.GetAssembly( typeof( Person ) );
but the its not my dll its ididnt write the class
i have a DLL on my machine
C:myDLL.dll
and i want to know if its signd
and if it was signed i want to know the
“signer name ” and the “time stamp”
i wrote this code and it works fine
but
the “cert ” has alot of data
in it but i cant find
“signer name ” and the “time stamp”
//====================================
public bool signed(string filename)
{
X509Certificate cert = null;
try
{
cert = X509Certificate.CreateFromSignedFile(filename);
}
catch (CryptographicException e)
{
return false;
}
return true;
}
//========================
sorry man for all these questions but i cant find any thing about “time stamp” and “signer name”
and thanx a lot
Hi,
I am stepping in a bit late, but the signer name can be found in the Issuer property of the X509 certificate.
And the certificate has a ValidFrom and ValidTo properties. I’m not sure this is what you’re looking for as a timestamp.
Anyway, you can also retrieve an Assembly object from a file (wihtout using a type) with the Assembly.LoadFile() method.
Regards