探究數字證書 公鑰 私鑰在IdentityServer4中和ADFS+Sharepoint中的使用
#Linux系統生成證書:(推薦使用) sudo yum install openssl (CentOS) #生成私鑰檔案 openssl genrsa -out idsrv4.key 2048 #建立證書籤名請求檔案 CSR(Certificate Signing Request),用於提交給證書頒發機構(即 Certification Authority (CA))即對證書籤名,申請一個數字證書。 openssl req -new -key idsrv4.key -out idsrv4.csr #生成自簽名證書(證書頒發機構(CA)簽名後的證書,因為自己做測試那麼證書的申請機構和頒發機構都是自己,crt 證書包含持有人的資訊,持有人的公鑰,以及簽署者的簽名等資訊。當用戶安裝了證書之後,便意味著信任了這份證書,同時擁有了其中的公鑰。) openssl x509 -req -days 365 -in idsrv4.csr -signkey idsrv4.key -out idsrv4.crt (包含公鑰) #自簽名證書與私匙合併成一個檔案(注:.pfx中可以加密碼保護,所以相對安全些) openssl pkcs12 -export -in idsrv4.crt -inkey idsrv4.key -out idsrv4.pfx (注:在生成的過程中會讓我們輸入Export Password)
或者
openssl req -newkey rsa:2048 -nodes -keyout idsrv4.key -x509 -days 365 -out idsrv4.cer
openssl pkcs12 -export -in idsrv4.cer -inkey idsrv4.key -out idsrv4.pfx
都可以生成證書
只有pfx格式的數字證書是包含有私鑰的,cer格式的數字證書裡面只有公鑰沒有私鑰。
所以在IdentityServer4中
services.AddIdentityServer() //.AddDeveloperSigningCredential() .AddSigningCredential(new X509Certificate2(Path.Combine(basePath, Configuration["Certificates:CerPath"]), Configuration["Certificates:Password"]))
這裡的證書是pfx的證書,so 通過私鑰來簽名,在訪問API的時候 ,通過這個證書來獲取公鑰進行驗籤。
而在SharePoint ADFS中
可以看到
裡面的令牌簽名是cer的 ,cer意味著只有公鑰,上面還有個令牌解密,應該是私鑰,然後把這個公鑰頒給了SharePoint
在SharePoint 伺服器上,以管理員身份啟動SharePoint 命令列管理程式:
序號 |
命令說明 |
執行命令 |
1 |
設定ADFS簽名證書變數,此處Tokensign.cer證書是從ADFS伺服器上匯出的令牌簽名證書 |
$cert = New-Object System.Security.Cryptography.x509Certificates.x509Certificate2 (“\\ad-test\Software\Tool\Tokensign.cer") |
2 |
將令牌簽名證書匯入到SharePoint的信任區 |
New-SPTrustedRootAuthority -Name "Token Signing Cert" -Certificate $cert |
3 |
設定UPN宣告變數
|
$upnClaimMap = New-SPClaimTypeMapping -IncomingClaimType "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn" -IncomingClaimTypeDisplayName "UPN" -SameAsIncoming
|
|
設定通用名宣告變數
|
$cnNameMap = New-SPClaimTypeMapping -IncomingClaimType "http://schemas.xmlsoap.org/claims/CommonName" -IncomingClaimTypeDisplayName "Display Name" –SameAsIncoming
|
4 |
設定“realm”宣告變數,此處一定要與ADFS信賴方信任配置的“信賴方識別符號”一致,包括大小寫
|
$realm = “urn:Dev2:SP2013”
|
5 |
設定登入URL變數,此處為ADFS登入的頁URL |
$signInURL = “https://adfs.****.com/adfs/ls” |
6 |
|
$ap = New-SPTrustedIdentityTokenIssuer -Name "ADFS Web SSO for SharePoint" -description “ADFS Web SSO for SharePoint" -realm $realm -ImportTrustCertificate $cert -ClaimsMappings $upnClaimMap, $cnNameMap -SignInURL $signInURL -IdentifierClaim $upnClaimMap.InputClaimType
|
7 |
|
Get-SPTrustedIdentityTokenIssuer |
其實原理都是一樣的,一個ADFS將token 用私鑰簽名 ,sharepoint 中的公鑰驗籤。