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)
12837정성태9/11/20218293VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217899Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20219145.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217559오류 유형: 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/20216967VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217868VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216395VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218681오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110272.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
12828정성태9/4/20218419.NET Framework: 1114. C# 10 - (13) 단일 파일 내에 적용되는 namespace 선언파일 다운로드1
12827정성태9/4/20218346스크립트: 27. 파이썬 - 웹 페이지 데이터 수집을 위한 scrapy Crawler 사용법 요약
12826정성태9/3/202110589.NET Framework: 1113. C# 10 - (12) 문자열 보간 성능 개선 [1]파일 다운로드1
12825정성태9/3/20218201개발 환경 구성: 603. GoLand - WSL 환경과 연동
12824정성태9/2/202117273오류 유형: 760. 파이썬 tensorflow - Dst tensor is not initialized. 오류 메시지
12823정성태9/2/20216916스크립트: 26. 파이썬 - PyCharm을 이용한 fork 디버그 방법
12822정성태9/1/202112134오류 유형: 759. 파이썬 tensorflow - ValueError: Shapes (...) and (...) are incompatible [2]
12821정성태9/1/20217744.NET Framework: 1112. C# - .NET 6부터 공개된 ISpanFormattable 사용법
12820정성태9/1/20218057VC++: 147. Golang - try/catch에 대응하는 panic/recover [1]파일 다운로드1
12819정성태8/31/20218154.NET Framework: 1111. C# - FormattableString 타입
12818정성태8/31/20217389Windows: 198. 윈도우 - 작업 관리자에서 (tensorflow 등으로 인한) GPU 연산 부하 보는 방법
12817정성태8/31/20219978스크립트: 25. 파이썬 - 윈도우 환경에서 directml을 이용한 tensorflow의 AMD GPU 사용 방법
12816정성태8/30/202115352스크립트: 24. 파이썬 - tensorflow 2.6 NVidia GPU 사용 방법 [2]
12815정성태8/30/20218462개발 환경 구성: 602. WSL 2 - docker-desktop-data, docker-desktop (%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx) 파일을 다른 디렉터리로 옮기는 방법
12814정성태8/30/202110756.NET Framework: 1110. C# 11 - 인터페이스 내에 정적 추상 메서드 정의 가능 (DIM for Static Members) [2]파일 다운로드1
12813정성태8/29/20218973.NET Framework: 1109. C# 10 - (11) Lambda 개선파일 다운로드1
12812정성태8/28/20218598.NET Framework: 1108. C# 10 - (10) 개선된 #line 지시자
... 31  [32]  33  34  35  36  37  38  39  40  41  42  43  44  45  ...