如何進(jìn)行IdentityServer4 RSA 證書加密,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
蕪湖ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
IdentityServer4 默認(rèn)提供了兩種證書加密配置:
services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTemporarySigningCredential();
這兩種證書加密方式,都是臨時(shí)使用,每次重啟項(xiàng)目的時(shí)候,都會(huì)重新生成一個(gè)新的證書,這時(shí)候就會(huì)導(dǎo)致一個(gè)問題,重啟之前生成的access_token
,在重啟之后,就不適用了,因?yàn)樽C書改變了,對(duì)應(yīng)的加密方式也改變了,所以,就會(huì)出現(xiàn)下面這個(gè)問題:
錯(cuò)誤信息:
Www-Authenticate:Bearer error="invalid_token", error_description="The signature key was not found"
解決方式,就是始終使用一個(gè)證書,我們先這樣配置:
services.AddIdentityServer() .AddDeveloperSigningCredential();
可以查看下AddDeveloperSigningCredential
實(shí)現(xiàn)源碼,啟動(dòng)項(xiàng)目,IdentityServer4 會(huì)在項(xiàng)目目錄中生成一個(gè)tempkey.rsa
證書文件:
{ "KeyId": "4e1765de45ef639261115198826dfea7", "Parameters": { "D": "FnB7kIinBgoZDaRqIrRQHEF45FBF9amOrTn8oFdmsxPqJbh21bHeCw11AtCCC4p1mm750onDXeP+yoBHymr/wNn40VmGdhR4hnObHhhw5pyQKECIS41DFDatCZif9uhDgHsOvYHMRVNSapDFoDUvbTE6t7rv4prn2fDt5mzRD9AqdT2HyTcwa/H1haaZNwmy3UevYYy8ya4kKXvjRo6+O7BMBh+yBvHgezQ57Ye/N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ybA8n1OyPXHdOyuSWOiVp69ibo5OOXLL4iWzY1VOouXeZrYimxNPvVKlRf8AsVcv3n/0/FEhwY9gnQm4PZYUGwQ96WZ5Z/CWJ9xTORg54Wh79hk0=", "Q": "7Gmr/h43bM+9W4Ygh+tNh4/etECuT/RQ1LMS5uBXxXdvUl6wSm2+ec/CBRobxVHG2pDXdr0pegn0Yz4MprsLtS5KvFg6yopI3Y3TptTGNZPtbd1O7P4i6b+RNOYCq0Y99mkGofqAlAMnDG+SA2EJN2ugPjLelC7GWtfzNG5NMgU=" } }
AddDeveloperSigningCredential
中代碼執(zhí)行,會(huì)先判斷tempkey.rsa
證書文件是否存在,如果不存在的話,就創(chuàng)建一個(gè)新的tempkey.rsa
證書文件,如果存在的話,就使用此證書文件。
所以,我們配置的時(shí)候,傳遞一個(gè)tempkey.rsa
證書文件名,就可以了:
services.AddIdentityServer() .AddDeveloperSigningCredential("tempkey.rsa");
當(dāng)然,你也可以對(duì)tempkey.rsa
證書文件,進(jìn)行重命名操作。
看完上述內(nèi)容,你們掌握如何進(jìn)行IdentityServer4 RSA 證書加密的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!