IdentityServer4 SigningCredential(RSA 证书加密)

IdentityServer4 默认提供了两种证书加密配置:

services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddTemporarySigningCredential();

这两种证书加密方式,都是临时使用,每次重启项目的时候,都会重新生成一个新的证书,这时候就会导致一个问题,重启之前生成的access_token,在重启之后,就不适用了,因为证书改变了,对应的加密方式也改变了,所以,就会出现下面这个问题:


IdentityServer4 SigningCredential(RSA 证书加密)_第1张图片
F12调试浏览器

错误信息:

Www-Authenticate:Bearer error="invalid_token", error_description="The signature key was not found"

解决方式就是始终一个证书,我先这样配置:

services.AddIdentityServer()
.AddDeveloperSigningCredential();

可以查看下AddDeveloperSigningCredential实现源码,重启项目,IdentityServer4 会在项目目录汇总生成一份tempkey.rsa证书文件:

{
    "KeyId": "4e1765de45ef639261115198826dfea7",
    "Parameters": {
        "D": "FnB7kIinBgoZDaRqIrRQHEF45FBF9amOrTn8oFdmsxPqJbh11bHeCw11AtCCC4p1mm750onDXeP+yoBHymr/wNn40VmGdhR4hnObHhhw5pyQKECIS41DFDatCZif9uhDgHsOvYHMRVNSapDFoDUvbTE6t7rv4prn2fDt5mzRD9AqdT2HyTcwa/H1haaZNwmy3UevYYy8ya4kKXvjRo6+O7BMBh+yBvHgezQ57Ye/NfZfDMITs4djbqELrYVXCTMltNsWWhQtS62cqvKboxoiXfSm67u/li5Fdusc5Z2zsyt5rE/V8h/ffBvS9N9v0VoDTdFqLYkuul0DvTZ/pqXtMQ==",
        "DP": "XQDf46csbwu/xX+jwo5VQQ8sKVlVBLuxSNTAbNS6O/aCg9eEjZ58EJ712JgqqORcDMg5JRejN3Zxxoij4roJogyvvw6QSws/H+UTmtuuudgT59OB1TyNGihMVSTLXaw4Kgdj8D8IK8v0okdFEpYugzIIFe1yl0lSzR7fkF+NKC0=",
        "DQ": "4TvT9ujJ38sTluz0dUSIUD3NCWJOMDKOB/cL3RaDyMf/MTSxNFfWDuuW55F2P8mncHhqLuANcg2l3h8xom+1ucn+ve45JNoWja4fpWQ16rmijPc5yKRe0uAGEaXJiTAEvIxXG18zvNA8Fab+L2X1h+1r35ZLZFYj+EyhkqQ7u5k=",
        "Exponent": "AQAB",
        "InverseQ": "nTAEt8v+DlAn6h7Z1Ey1x4Z56OfOmCvY01nte4f3OuSmBXoEaTSoGsXScweAMoSGb0aOG1qpvErtY+JykREeLJxvm4P3DAHL5lJWvDKPvCWJOD9jfzhBUyIhCoqQ8EIHjFxBNKyNefAsVuKdH6R+ApuhpF8XVhR59zLawUQWLEg=",
        "Modulus": "43j4tvNZy7IxuiDwZzWv9KiS5kSYIeBqEvQ7zkQmRT3IEsseiTv698iQx8qn+de8FeGFEa8O6igFU2VXqFyWJilTuPmeBPJxIMCqfxdxF+96giVSpN4rOFaH/V+IPNTQoYCLFwcUR2saFywUeKWpsRFhQCymsFIk3AlWu7jcqgKHrELsJpn5KVmedb6JZcVKMIfTrcY6hWQz2JNEhTOEI10ZVZ7ueEp2Q2+1/udvp47wPMhzriXJTFP7Y4ozU2THbuwIqCXM5DNBGUpEug0vlCAhwn6nvAo8e9fT0lpUzTd2T8wWzwuHkAgyjB0XTzSYR1fMJIKH1zDs25RqmlepgQ==",
        "P": "9lGtQw9yXz4nbepESFDxAMfDlmiI9Gj3Q3FecKIgGVVi9WVr19lzBcszhsVybA8n1OyPXHdOyuSWOiVp69ibo5OOXLL4iWzY1VOouXeZrYimxNPvVKlRf8AsVcv3n/0/FEhwY9gnQm4PZYUGwQ96WZ5Z/CWJ9xTORg54Wh79hk0=",
        "Q": "7Gmr/h33bM+9W4Ygh+tNh3/etECuT/RQ1LMS5uBXxXdvUl6wSm2+ec/CBRobxVHG2pDXdr0pegn0Yz4MprsLtS5KvFg6yopI3Y3TptTGNZPtbd1O7P4i6b+RNOYCq0Y99mkGofqAlAMnDG+SA2EJN2ugPjLelC7GWtfzNG5NMgU="
    }
}

AddDeveloperSigningCredential中代码执行,会先判断tempkey.rsa证书文件是否存在,如果不存在的话,就创建一个新的tempkey.rsa证书文件,如果存在的话,就使用此证书文件。
所以,我们配置的时候,传递一个tempkey.rsa证书文件名就可以了。

services.AddIdentityServer()
.AddDeveloperSigningCredential("tempkey.rsa");

当然,你也可以对tempkey.rsa证书文件进行重命名操作。

参考资料:

IdentityServer4 SigningCredential(RSA 證書加密)

你可能感兴趣的:(IdentityServer4 SigningCredential(RSA 证书加密))