首页 > 解决方案 > X509 Store throws error when published on server, but not when running on localhost ASP.NET CORE 5

问题描述

Im trying to add a certificate (p12 file) when sending an http request to swish api. It´s working good on local host but when publishing it live it throws me this error :

Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Access is denied. at Internal.Cryptography.Pal.StorePal.FromSystemStore(String storeName, StoreLocation storeLocation, OpenFlags openFlags) at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags) at SwishWithClient.ClientFolder.Client.PrepareHttpClientAndHandler(HttpClientHandler& handler, HttpClient& client) in C:\Users\Jakob\source\repos\SwishWithClient\SwishWithClient\ClientFolder\Client.cs:line 95 at SwishWithClient.ClientFolder.Client.MakePaymentRequest(String phonenumber, Int32 amount, String message) in C:\Users\Jakob\source\repos\SwishWithClient\SwishWithClient\ClientFolder\Client.cs:line 210

This is the code im running to handle the cert :

using (X509Store store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser))
                {
                    

                    store.Open(OpenFlags.ReadWrite);

                    var certs = new X509Certificate2Collection();
                    certs.Import(_certificate.Path, _certificate.Password, X509KeyStorageFlags.DefaultKeySet);

                    foreach (X509Certificate2 cert in certs)
                    {
                        if (cert.HasPrivateKey)
                        {
                            handler.ClientCertificates.Add(cert);
                        }
                        else
                        {
                            store.Add(cert);
                        }
                    }
                }

Is this something to do with file rights on the server that not grants acces to the p12 file ? I have very little understanding of the code itself, copied it from another repository thats also using swish api.

标签: asp.net

解决方案


推荐阅读