brutaldev / StrongNameSigner

Strong-name sign third party .NET assemblies without the source code.
https://brutaldev.com/post/net-assembly-strong-name-signer
Other
327 stars 68 forks source link

Does not work with VS 2022 started with Admin rights targeting .net 4.8 framework #94

Closed rboy1 closed 1 year ago

rboy1 commented 1 year ago

When trying to use the nuget add on with VS 2022 with a target framework of 4.8, it doesn't seem to work. For example when including this unsigned nuget package https://www.nuget.org/packages/OSVersionExt/ in the project it, it doesn't strong name it leading to an error when the strong named project tries to access it.

brutaldev commented 1 year ago

@rboy1 Works just fine here.

  1. Created a new VS 2022 console app targeting .NET 4.8.
  2. Added NuGet package for OSVersionExt
  3. Added sample code and ran to test output successfully.
  4. Enable strong name signing: image
  5. Rebuilt to verify the reference error: issue94-1
  6. Added Brutal.Dev.StrongNameSigner NuGet package (version 3.3.3).
  7. Rebuild the solution:
    Rebuild started...
    1>------ Rebuild All started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
    1>  -- Starting Brutal Developer .NET Assembly Strong-Name Signer Task --
    1>  Adding 'C:\Dev\ConsoleApp1\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll' for processing.
    1>  1. Loading assemblies...
    1>  2. Checking assembly references...
    1>     Checking assembly references in 'C:\Dev\ConsoleApp1\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
    1>  3. Strong-name unsigned assemblies...
    1>     Signing assembly 'C:\Dev\ConsoleApp1\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
    1>  4. Fix InternalVisibleToAttribute references...
    1>  5. Fix CustomAttributes with Type references...
    1>  6. Fix BAML references...
    1>  7. Save assembly changes...
    1>     Saving changes to assembly 'C:\Dev\ConsoleApp1\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
    1>  8. Cleanup...
    1>  -- Finished Brutal Developer .NET Assembly Strong-Name Signer Task in 00:00:00.6701494 -- 
    1>C:\Dev\ConsoleApp1\Program.cs(27,50,27,96): warning CS0618: 'MajorVersion10Properties.ReleaseId' is obsolete: 'Works until Windows 10 version 2009/20H2 (build 19042) only. Use DisplayVersion instead.'
    1>  ConsoleApp1 -> C:\Dev\ConsoleApp1\bin\Debug\ConsoleApp1.exe
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
    ========== Elapsed 00:01.548 ==========
  8. Run application to verify functionality and it works perfectly with no warnings.

Sample project to verify this yourself if you want: Issue94.zip

You need to post the output for the signer if there are any errors trying to sign your file.

rboy1 commented 1 year ago

Thanks for looking into this. It's still running into an error and I believe it's related to elevated permissions when running VS 2022 with Admin rights.

I tried running your attached solution and I got an error. (actually two, the first was that the AssemblyInfo.cs was missing which I took care of deleting the missing file from the solution), after compilation I got this:

System.IO.FileLoadException
  HResult=0x80131044
  Message=Could not load file or assembly 'OSVersionExt, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

The one point to note is that I'm starting VS 2022 with Admin rights (right click VS 2022 and Run as Administrator).

Debug output:

'ConsoleApp1.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ConsoleApp1.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\ConsoleApp1.exe'. Symbols loaded.
An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module.
Could not load file or assembly 'OSVersionExt, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)

The program '[2252] ConsoleApp1.exe' has exited with code 4294967295 (0xffffffff).

Build output

Build started...
------ Build started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
  -- Starting Brutal Developer .NET Assembly Strong-Name Signer Task --
  Adding 'C:\Users\xxx\OneDrive\Desktop\Issue94\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll' for processing.
  1. Loading assemblies...
  2. Checking assembly references...
     Checking assembly references in 'C:\Users\xxx\OneDrive\Desktop\Issue94\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
  3. Strong-name unsigned assemblies...
     Signing assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
  4. Fix InternalVisibleToAttribute references...
  5. Fix CustomAttributes with Type references...
  6. Fix BAML references...
  7. Save assembly changes...
     Saving changes to assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll'.
     Failed to save assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\packages\OSVersionExt.2.0.0\lib\net462\OSVersionExt.dll': Method not found: 'Void Mono.Cecil.WriterParameters.set_StrongNameKeyBlob(Byte[])'.
  8. Cleanup...
  -- Finished Brutal Developer .NET Assembly Strong-Name Signer Task in 00:00:00.0271211 -- 
C:\Users\xxx\OneDrive\Desktop\Issue94\Program.cs(27,50,27,96): warning CS0618: 'MajorVersion10Properties.ReleaseId' is obsolete: 'Works until Windows 10 version 2009/20H2 (build 19042) only. Use DisplayVersion instead.'
CSC : warning CS8002: Referenced assembly 'OSVersionExt, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.
  ConsoleApp1 -> C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\ConsoleApp1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Elapsed 00:01.058 ==========

If I start VS without admin rights it seems to work fine, however I need Admin rights for my project to work/debug.

Oddly enough it works with VS 2017 started with Admin rights (targeting .NET 4.6.1)

rboy1 commented 1 year ago

Also when trying to replicate the issue in Admin mode, after having signed it in non admin mode, I would recommend deleting the nuget package and restoring it again and then trying to build it in admin mode.

brutaldev commented 1 year ago

@rboy1 Deleted bin/obj/packages and opened the sample solution with elevated VS 2022 and there are no errors so I'm not going to investigate this any further. If I was to guess, it's because you are building in your OneDrive directory and files are probably being synched incorrectly.

rboy1 commented 1 year ago

Actually, I managed to replicate the same error even in non Admin mode. I deleted and restore the nuget package and tried to rebuild it in non admin mode (see attached screenshot, the Admin symbol on the right corner is missing) and it gave me the error.

image

Can't explain it, the first time after an Admin mode build failure it built it in non Admin mode. The second time even with Non admin mode it failed.

Build started...
------ Build started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
  -- Starting Brutal Developer .NET Assembly Strong-Name Signer Task --
  Adding 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\OSVersionExt.dll' for processing.
  1. Loading assemblies...
  2. Checking assembly references...
     Checking assembly references in 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\OSVersionExt.dll'.
  3. Strong-name unsigned assemblies...
     Signing assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\OSVersionExt.dll'.
  4. Fix InternalVisibleToAttribute references...
  5. Fix CustomAttributes with Type references...
  6. Fix BAML references...
  7. Save assembly changes...
     Saving changes to assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\OSVersionExt.dll'.
     Failed to save assembly 'C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\OSVersionExt.dll': Method not found: 'Void Mono.Cecil.WriterParameters.set_StrongNameKeyBlob(Byte[])'.
  8. Cleanup...
  -- Finished Brutal Developer .NET Assembly Strong-Name Signer Task in 00:00:01.5656551 -- 
C:\Users\xxx\OneDrive\Desktop\Issue94\Program.cs(27,50,27,96): warning CS0618: 'MajorVersion10Properties.ReleaseId' is obsolete: 'Works until Windows 10 version 2009/20H2 (build 19042) only. Use DisplayVersion instead.'
CSC : warning CS8002: Referenced assembly 'OSVersionExt, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.
  ConsoleApp1 -> C:\Users\xxx\OneDrive\Desktop\Issue94\bin\Debug\ConsoleApp1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Elapsed 00:03.129 ==========
rboy1 commented 1 year ago

@rboy1 Deleted bin/obj/packages and opened the sample solution with elevated VS 2022 and there are no errors so I'm not going to investigate this any further. If I was to guess, it's because you are building in your OneDrive directory and files are probably being synched incorrectly.

Not using OneDrive (it's disabled) but to test it out anyways I moved the solution to another folder and tried to rebuild it and it failed again with the same error. This isn't unique to me. I've seen this error reported by other users using a different package signer and it appears to be rooted in the Mono.Cecil error. Here's the reference

https://github.com/dsplaisted/strongnamer/issues/52

I'm able to replicate this issue easily here in Admin mode of VS 2022.

EDIT: Logs

Build started...
------ Build started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
  -- Starting Brutal Developer .NET Assembly Strong-Name Signer Task --
  Adding 'C:\Users\xxx\source\repos\Issue94\bin\Debug\OSVersionExt.dll' for processing.
  1. Loading assemblies...
  2. Checking assembly references...
     Checking assembly references in 'C:\Users\xxx\source\repos\Issue94\bin\Debug\OSVersionExt.dll'.
  3. Strong-name unsigned assemblies...
     Signing assembly 'C:\Users\xxx\source\repos\Issue94\bin\Debug\OSVersionExt.dll'.
  4. Fix InternalVisibleToAttribute references...
  5. Fix CustomAttributes with Type references...
  6. Fix BAML references...
  7. Save assembly changes...
     Saving changes to assembly 'C:\Users\xxx\source\repos\Issue94\bin\Debug\OSVersionExt.dll'.
     Failed to save assembly 'C:\Users\xxx\source\repos\Issue94\bin\Debug\OSVersionExt.dll': Method not found: 'Void Mono.Cecil.WriterParameters.set_StrongNameKeyBlob(Byte[])'.
  8. Cleanup...
  -- Finished Brutal Developer .NET Assembly Strong-Name Signer Task in 00:00:00.8026522 -- 
C:\Users\xxx\source\repos\Issue94\Program.cs(27,50,27,96): warning CS0618: 'MajorVersion10Properties.ReleaseId' is obsolete: 'Works until Windows 10 version 2009/20H2 (build 19042) only. Use DisplayVersion instead.'
CSC : warning CS8002: Referenced assembly 'OSVersionExt, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.
  ConsoleApp1 -> C:\Users\xxx\source\repos\Issue94\bin\Debug\ConsoleApp1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Elapsed 00:02.442 ==========
rboy1 commented 1 year ago

For reference you will encounter this error on a fresh install of Windows 11 / VS 2022 with ONLY .net 4.8 installed. If you've installed .net 4.6.1 you won't encounter this error. It appears to have something related to MSBuildRuntimeType defaulting to net461 if you're building with a target that's not 4.6.1

rboy1 commented 1 year ago

FYR: https://github.com/dsplaisted/strongnamer/issues/47

I don't know if this is something that needs to detect at runtime by Stron-Name Signer to see which libraries are being loaded at runtime which may be causing the issue.

rboy1 commented 1 year ago

@brutaldev do you think it might be an issue with binding redirect? At runtime Strong-Name Signer isn't able to access the Mono-Cecil dll because it's being redirected to a different version of the assembly that doesn't exist. I'm asking because after updating to .NET 4.8 I'm seeing binding direct issues which never existed with .net 4.6.1