WindowsCryptographicException The system cannot find the file specified.


Azure – while trying to read pfx (certificate) from disk I came across a number of issues, but thought this might help some out.

- May 8, 2020

Rest of the Story:

Azure – while trying to read pfx (certificate) from disk I came across a number of issues, but thought this might help some out.

The fix was adding the final parameter X509KeyStorageFlags

var cert = new X509Certificate2(
    $"{Environment.ContentRootPath}/App_Data/mycert.pfx","{password}",
    X509KeyStorageFlags.MachineKeySet); 

Other references:

https://support.microsoft.com/en-us/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard-net-applic https://stackoverflow.com/questions/52750160/what-is-the-rationale-for-all-the-different-x509keystorageflags

An unhandled exception occurred while processing the request.
WindowsCryptographicException: The system cannot find the file specified. System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider, CngKeyOpenOptions openOptions)  
Exception: An error was encountered while handling the remote login. Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()  
Stack Query Cookies Headers Routing 
WindowsCryptographicException: The system cannot find the file specified. System.Security.Cryptography.CngKey.Open(string keyName, 
    CngProvider provider, CngKeyOpenOptions openOptions) 
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider) 
Internal.Cryptography.Pal.CertificatePal.GetPrivateKey<T>(Func<CspParameters, T> createCsp, Func<CngKey, T> createCng) 
Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey() 
System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() 
Microsoft.IdentityModel.Tokens.X509SecurityKey.get_PrivateKey() 
Microsoft.IdentityModel.Tokens.X509SecurityKey.get_PrivateKeyStatus() 
Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider.FoundPrivateKey(SecurityKey key) 
Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(SecurityKey key, string algorithm, bool willCreateSignatures) 
Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(SecurityKey key, string algorithm, bool willCreateSignatures, 
        CryptoProviderFactory cryptoProviderFactory) 
Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateSignatureProvider(SecurityKey key, string algorithm, bool willCreateSignatures) 
Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateForSigning(SecurityKey key, string algorithm)  Microsoft.IdentityModel.JsonWebTokens.JwtTokenUtilities.CreateEncodedSignature(string input, SigningCredentials signingCredentials) System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.WriteToken(SecurityToken token)  Sample.Idp.Startup+<>c__DisplayClass7_0.<ConfigureServices>b__8(AuthorizationCodeReceivedContext context) in Startup.cs
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents.AuthorizationCodeReceived(AuthorizationCodeReceivedContext context) Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RunAuthorizationCodeReceivedEventAsync(OpenIdConnectMessage authorizationResponse, ClaimsPrincipal user, AuthenticationProperties properties, JwtSecurityToken jwt) 
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync()