runtime: Loading PFX without admin causes CryptographicException
Description
After upgrading to .Net 9.0 on a API project the following codes throws the error “System.Security.Cryptography.CryptographicException: ‘Access denied.’” when not run as Admin. This works fine without admin on .net 8.0.
builder.WebHost.UseKestrel(options =>
{
options.ListenAnyIP(443, builder =>
{
builder.UseHttps("D:\\private_cert.pfx", "password");
});
});
Reproduction Steps
- Create a new .Net 9.0 API project.
- In Program.cs load a certifcate using the code provided
Expected behavior
Project runs without issue
Actual behavior
Exception thrown: System.Security.Cryptography.CryptographicException: ‘Access denied.’
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.ImportPfx(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12NoLimits(System.ReadOnlyMemory<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, ref System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return earlyReturn) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(System.ReadOnlyMemory<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadFromFile<System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return>(string path, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits, System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadFromFileFunc<System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return> loader) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12PalFromFile(string path, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.CertificatePal.FromBlobOrFile(System.ReadOnlySpan<byte> rawData, string fileName, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) Unknown
System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509Certificate.X509Certificate(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) Unknown
Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions listenOptions, string fileName, string password) Unknown
> Program.<Main>$.AnonymousMethod__0_18(Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions builder) Line 52 C#
Regression?
Yes, works on .Net 8.0
Known Workarounds
No response
Configuration
.Net 9.0 Windows 11 26100.2314 x64
Other information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 8
- Comments: 22 (6 by maintainers)
I was able to get the file method working by converting the pfx file to a p12 file.
This approach was mentioned in on this comment https://stackoverflow.com/a/73095429.
I don’t understand enough about the pfx/p12 difference to know whether this is a good approach or not but it resolved my problem without changing system folder permissions and without running as admin.
Update the path to reference the
cert.p12file.The app then ran ok without admin privileges and used the specified cert in the HTTPS connection.
In case it’s relevant in terms of reproducing the error - we run as non admin users and use a distinct account to elevate to admin.
same issue, but when I was trying to create a self-signed cert. I have the following piece of code, which worked fine on .NET8
Currently on .NET9, creation of
newCertfails with the following stack -I verified that running it as admin solves this but sadly, I cannot run this code as admin as the nominal flow. Any other workarounds?
We’ve got the same issue after .net 9 upgrade. Even when using the X509KeyStorageFlags option I still get “Access denied”. Only workaround is to run as admin ☹
I was having the same issue with:
new X509Certificate2(path, password);And when using the X509CertificateLoader I got an exception (Win32Exception: The credentials supplied to the package were not recognized) when the connection was made, because the private key was not loaded correctly:
X509CertificateLoader.LoadPkcs12FromFile(path, password)I was able to solve the issue by adding X509KeyStorageFlags:
X509CertificateLoader.LoadPkcs12FromFile(path, password,, X509KeyStorageFlags.UserKeySet);@krwq - any update on this ?
We have this code, works fine in .NET 8 (admin and normal mode). Moving to .NET 9 , it works in admin mode. In normal mode , getting access denied.
Edit: It works fine on MacOS , is specific to Windows
AFAIK p12 and pfx should be both PKCS#12. The only reason I can see this could help is re-export - I’m guessing something changed in the inner structure which causes it to now work.
@krwq
I tried upgrading to .NET 9 and am facing the exact same issue. Here is how the certificate is created and loaded:
appsettings.Development.json:Local dev wildcard SSL certificate
Create Certificate from Powershell
Configure permissions in Windows Certificate Store
mmc.exe.Add Certificates Snap-InCertificatesand Add. SelectComputer Accountand thenLocal Computerand Finish.PersonalthenCerticicatesand you can seemydomain.local.All Tasks / Manager Private Keys. AddUserswith “Read Access”.Trusted People / CertificatesPrevious “Access Denied” resolution (but not working here)
You need Read/Write on
Everyone. See MS documentation and Stack Overflow Post, otherwise you will get the following error in the console when running the Web Server when running as non-Admin:But the current .NET 9 error is:
“Workarounds”
OR
Non-admin by loading from the Certificate Store in
appsettings.Development.json:I have a different scenario which is running into trouble with .NET9 and which is failing for potentially the same reason.
I understand that
… now is obsolete with .NET9, which is why I replaced it by
The certificate itself is used by the Kestrel server:
This works fine if the Kestrel server is running on the target (Linux) server. However, for development and debugging purposes I’m running the Kestrel server on my local Windows 11 machine as well where a SSH script reroutes the remote server port to my local machine.
Code above only works on my local machine if I’m running Visual Studio as administrator. The local Kestrel server can’t be reached if running as non-admin.
Can you please confirm/deny that the source of trouble is the same as reports above? Thanks in advance
Same issue here: dotnet/aspnetcore#59300