Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 10개 있습니다.)
개발 환경 구성: 555. openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법
; https://www.sysnet.pe.kr/2/0/12570

개발 환경 구성: 565. PowerShell - New-SelfSignedCertificate를 사용해 CA 인증서 생성 및 인증서 서명 방법
; https://www.sysnet.pe.kr/2/0/12588

개발 환경 구성: 654. openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법 (2)
; https://www.sysnet.pe.kr/2/0/13187

개발 환경 구성: 662. openssl - 윈도우 환경의 명령행에서 SAN 적용하는 방법
; https://www.sysnet.pe.kr/2/0/13235

개발 환경 구성: 663. openssl을 이용해 인트라넷 IIS 사이트의 SSL 인증서 생성
; https://www.sysnet.pe.kr/2/0/13236

개발 환경 구성: 681. openssl - 인증서 버전(V1 / V3)
; https://www.sysnet.pe.kr/2/0/13371

개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
; https://www.sysnet.pe.kr/2/0/13442

개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
; https://www.sysnet.pe.kr/2/0/13443

Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
; https://www.sysnet.pe.kr/2/0/13445

닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
; https://www.sysnet.pe.kr/2/0/13447




OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법

이번 글은 IIS 버전을 새롭게 정리한 것입니다.

openssl을 이용해 인트라넷 IIS 사이트의 SSL 인증서 생성
; https://www.sysnet.pe.kr/2/0/13236




지난 글에서,

비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
; https://www.sysnet.pe.kr/2/0/13442

호스팅하는 ASP.NET 웹 사이트는 기본적으로 "ASP.NET Core HTTPS development certificate" 테스트 인증서를 사용하기 때문에 "localhost"로만 (매끄럽게) 접속할 수 있습니다.

이번엔, (vcpkg로 쉽게 빌드할 수 있는) openssl로 만든 인증서 파일을 활용해 그럴싸하게 호스팅 해보겠습니다. ^^




자, 그럼 우선 CA 인증서와 그것으로 서명한 "테스트 웹 사이트용 인증서"를 openssl로,

openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법 (2)
; https://www.sysnet.pe.kr/2/0/13187

이렇게 만들어 줍니다.

// 참고로, 공인 CA 업체로부터 인증서를 받는다면 아래에서 1~2번 작업만 사용자가 하고, 3번부터는 CA 측에서 담당합니다.

// 1) 웹 사이트 개인키 생성
c:\temp\cert> openssl genrsa -out test_site_cert.key 2048
// 2) CA에서 서명 받아 생성할 인증서의 요청 파일 생성
c:\temp\cert> openssl req -key test_site_cert.key -new -out test_site_cert.csr -subj "/CN=test.testhvpc.com"

// 3) CA 측에서 해당 인증서에 추가할 SAN 이름을 갖는 설정 파일 준비
c:\temp\cert> type ssl_conf.txt
subjectAltName=DNS:test.testhvpc.com,DNS:localhost,IP:127.0.0.1,IP:172.18.208.1

// 4) CA 인증서와 개인키 생성
c:\temp\cert> openssl req -newkey rsa:2048 -nodes -keyout test_ca.key -x509 -days 365 -out test_ca.crt  -subj "/CN=testca"

// 5) CA의 개인키로 인증서 요청을 승인(서명)해 인증서 생성
c:\temp\cert> openssl x509 -req -days 3650 -extfile ssl_conf.txt -in test_site_cert.csr -CA test_ca.crt -CAkey test_ca.key -CAcreateserial -out test_site_cert.crt

subjectAltName으로 다양한 값을 주었는데요, (여러분들의 환경에 맞게) 원하는 값을 다중으로 저렇게 넣으면 됩니다. 여기까지 완료했으면, 이제 생성된 파일 중 의미 있는 파일은 다음과 같습니다.

test_site_cert.crt  // 테스트 사이트 인증서 (Web 사이트에서 사용)
test_site_cert.key  // 테스트 사이트 개인키 (Web 사이트에서 사용)

test_ca.crt  // CA 인증서 (클라이언트 측에서 사용)
test_ca.key  // CA 개인키 (향후, 또 다른 인증서 요청을 수락하기 위해 사용)




ASP.NET 웹 프로젝트에 test_site_cert.crt, test_site_cert.key 파일을 복사하고 빌드 시 복사되도록 csproj에 다음과 같이 추가합니다.

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
        <None Update="test_site_cert.crt">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="test_site_cert.key">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>

그다음 appsettings.json 파일에 SSL 통신을 위한 인증서/개인키 파일과 Url 정보를 추가합니다.

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "Kestrel": {
        "EndPoints": {
            "Http": {
                "Url": "http://0.0.0.0:5001"
            },
            "HttpsFromPem": {
                "Url": "https://0.0.0.0:7252",
                "Certificate": {
                    "Path": "test_site_cert.crt",
                    "KeyPath": "test_site_cert.key"
                }
            }
        }
    }
}

일단, 서비스하는 측에서의 준비는 끝났습니다. 마지막으로, 연결하려는 측, 예를 들어 웹 브라우저로 접속한다면 그것이 실행될 컴퓨터에 "test_ca.crt" CA 공개키 인증서를 복사해 "Trusted Root Certification Authorities"에 등록해 줍니다.

여기까지 잘 마쳤으면, ^^ 이제 Visual Studio에서 F5 키를 눌러 웹 사이트를 실행한 후,

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'https://localhost:7252, http://localhost:5001'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5001
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://0.0.0.0:7252
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\temp\WebApplication1\WebApplication1

(CA 공개키 인증서를 설치한 PC 측의) 웹 브라우저를 이용해 SAN에 설정했던 값, 제 경우에는 localhost의 IP가 172.18.208.1이기 때문에 "https://172.18.208.1:7252"로 접속하면 정상적으로 (아무 부가 작업 없이) SSL로 호스팅되는 것을 확인할 수 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 11/14/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12856정성태11/23/20218901.NET Framework: 1121. C# - 동일한 IP:Port로 바인딩 가능한 서버 소켓 [2]
12855정성태11/13/20216246개발 환경 구성: 605. Azure App Service - Kudu SSH 환경에서 FTP를 이용한 파일 전송
12854정성태11/13/20217803개발 환경 구성: 604. Azure - 윈도우 VM에서 FTP 여는 방법
12853정성태11/10/20216168오류 유형: 766. Azure App Service - JBoss 호스팅 생성 시 "This region has quota of 0 PremiumV3 instances for your subscription. Try selecting different region or SKU."
12851정성태11/1/20217559스크립트: 34. 파이썬 - MySQLdb 기본 예제 코드
12850정성태10/27/20218734오류 유형: 765. 우분투에서 pip install mysqlclient 실행 시 "OSError: mysql_config not found" 오류
12849정성태10/17/20217869스크립트: 33. JavaScript와 C#의 시간 변환 [1]
12848정성태10/17/20218820스크립트: 32. 파이썬 - sqlite3 기본 예제 코드 [1]
12847정성태10/14/20218664스크립트: 31. 파이썬 gunicorn - WORKER TIMEOUT 오류 발생
12846정성태10/7/20218419스크립트: 30. 파이썬 __debug__ 플래그 변수에 따른 코드 실행 제어
12845정성태10/6/20218247.NET Framework: 1120. C# - BufferBlock<T> 사용 예제 [5]파일 다운로드1
12844정성태10/3/20216251오류 유형: 764. MSI 설치 시 "... is accessible and not read-only." 오류 메시지
12843정성태10/3/20216698스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202125053오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218522스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218621.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219672.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219325.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218231VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217842Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20219078.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217479오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216922VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217782VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216318VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218580오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...