your programing

"내부 오류가 발생했습니다."

lovepro 2020. 10. 16. 07:53
반응형

"내부 오류가 발생했습니다." X509Certificate2로 pfx 파일을로드 할 때


자체 서명 된 인증서 (C #)를 사용하려고합니다.

X509Certificate2 cert = new X509Certificate2(
    Server.MapPath("~/App_Data/myhost.pfx"), "pass");

공유 웹 호스팅 서버에서 오류가 발생했습니다.

System.Security.Cryptography.CryptographicException: An internal error occurred.

스택 추적은 다음으로 끝납니다.

System.Security.Cryptography.CryptographicException.
    ThrowCryptogaphicException(Int32 hr) +33
System.Security.Cryptography.X509Certificates.X509Utils.
    _LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, 
        Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.
    LoadCertificateFromFile(String fileName, Object password, 
        X509KeyStorageFlags keyStorageFlags) +237
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(
    String fileName, String password) +131

내 dev 컴퓨터에서 정상적으로로드됩니다. 개인 키 액세스가 필요하기 때문에 * .cer 파일이 아닌 * .pfx를로드하는 이유 (cer 파일로드 확인). 내 dev mochine에서 pfx를 다음과 같이 만들었습니다.

makecert -r -n "CN=myhost.com, E=admin@myhost.com" -sky exchange -b 01/01/2009
    -pe -sv myhost.pvk myhost.cer
<b>pvk2pfx</b> -pvk myhost.pvk -spc myhost.cer -pfx myhost.pfx -po pass</code>

버전 v5.131.3790.0의 makecert를 사용하고 있습니다.


개인 키에 로컬 컴퓨터 저장소를 사용하십시오.

X509Certificate2 cert = new X509Certificate2("myhost.pfx", "pass",
    X509KeyStorageFlags.MachineKeySet);

MachineKeySet"개인 키는 현재 사용자 저장소가 아닌 로컬 컴퓨터 저장소에 저장 됨"으로 설명됩니다. 플래그가없는 기본값은 사용자 저장소에 배치하는 것입니다.

디스크에서 인증서를 읽고이를 오브젝트에 저장하더라도 개인 키는 여전히 Microsoft Cryptographic API Cryptographic Service Provider 키 데이터베이스에 저장됩니다. 호스팅 서버에서 ASP.NET 프로세스에는 사용자 저장소에 액세스 할 수있는 권한이 없습니다.

또 다른 접근 방식 (아래 설명에 따라)은 IIS 구성 또는 앱 풀 ID를 수정하는 것입니다. 그러나 이는 해당되지 않을 수있는 이러한 구성 항목에 대한 액세스가 있다고 가정합니다 (예 : 공유 호스팅 환경에서).


Randy의 솔루션을 MachineKeySet으로 변경하려고 시도했지만 오류 메시지가 나타납니다.

"지정된 상태에서 사용할 수없는 키"

그래서 약간의 인터넷 검색을 한 후 다음과 같이 변경하라는 게시물을 찾았습니다.

var certificate = new X509Certificate2(certKeyFilePath, passCode, 
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet |       
X509KeyStorageFlags.PersistKeySet );

그리고 이것은 내 문제를 분류했습니다.

IIS 구성에서 설정 앱 풀 설정을 변경하라는 제안을 아직 시도하지 않았습니다. 이렇게하려면 사이트의 앱 풀에 대한 고급 설정으로 이동 한 다음 "사용자 프로필로드"를 true로 설정합니다. 이 설정이 false이면 키 컨테이너에 분명히 액세스 할 수 없습니다.

참고 URL : https://stackoverflow.com/questions/1345262/an-internal-error-occurred-when-loading-pfx-file-with-x509certificate2

반응형