Search code examples
c#asp.net.netasp.net-core.net-core

MacOS C# X509Store access is denied


When trying to initialize an instance of the System.Security.Cryptography.X509Certificates.X509Store the following error occurs.

using var x509Store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);

Exception:

Unhandled exception. System.Security.Cryptography.CryptographicException: Access is denied. at System.Security.Cryptography.X509Certificates.StorePal.AppleTrustStore.OpenStore(StoreName storeName, StoreLocation location, OpenFlags openFlags) at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags)

This code is used inside the library used and is needed to add certificates. It works in Windows.


Solution

  • Take a look at this article about Cross-Platform Cryptography in .NET

    From the code you shared, it looks like you need write access to the Root Store of the CurrentUser, i.e. the second row in this table. It seems that this is not supported for macOS 💁🏻‍♂️

    enter image description here

    If you're able to change how you're using the library to get it to use the My Store instead of the Root Store, you should be good - as that is supported on macOS.

    If that's not an option, I would suggest contacting the library's authors to see if they can direct you towards a solution, or maybe even work with you on an improvement to their code.