zoukankan      html  css  js  c++  java
  • Identityserver4配置证书

    IS4中如果token的类型是JWT,则需要使用RS256算法生成非对称签名,这意味着必须使用私钥来签名JWT token,并且必须使用对应的公钥来验证token签名,即验证token是否有效。使用RS256可以保证IS4服务端是JWT的唯一签名者,因为IS4服务端是唯一拥有私钥的一方,前提是私钥不会被泄露。所以我们需要一个证书为我们提供私钥和公钥。在开发环境可以利用IS4的AddDeveloperSigningCredential方法生成RSA文件,RSA文件为我们提供私钥和公钥,但是RSA文件不够安全,打开文件可以直接看到公钥和私钥,在生产环境我们一般会生成证书来提供私钥和公钥。使用OpenSSL生成证书的方式如下:

    1、安装OpenSSL工具 ,官网下载地址:https://slproweb.com/products/Win32OpenSSL.html 

    2、在CMD中执行以下命令

    openssl req -newkey rsa:2048 -nodes -keyout cas.clientservice.key -x509 -days 365 -out cas.clientservice.cer

    执行上面命令之后,我们可以在C:UsersAndy目录下面找到cas.clientservice.cer和cas.clientservice.key两个文件

    下面的命令是将生成的证书和Key封装成一个文件,以便IdentityServer可以使用它们去正确地签名tokens,文件会生成在CMD执行目录下面“C:UsersAndy”
    openssl pkcs12 -export -in cas.clientservice.cer -inkey cas.clientservice.key -out IS4.pfx

    IS4.pfx是证书名称,可以自己修改,中途会提示让你输入Export Password,这个password在IS4中会用到,需要记下来。

    如果执行上面的命令报错,则需要配置环境变量,然后再执行命令:

    3、配置IS4的证书

    services.AddIdentityServer()
                        //.AddDeveloperSigningCredential(true, ConstanceHelper.AppSettings.CredentialFileName)
                        .AddSigningCredential(new X509Certificate2(Path.Combine(basePath,
                             configuration["Certificates:Path"]),
                             configuration["Certificates:Password"]))
                       .AddInMemoryApiResources(Config.GetApis())
                       .AddInMemoryIdentityResources(Config.GetIdentityResources())
                       .AddInMemoryClients(Config.GetClients())
                       .AddProfileService<ProfileService>()
                       .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
                       .AddCustomAuthorizeRequestValidator<CustomAuthorizeRequestValidator>();

    4、在appsettings.json中添加证书配置信息

    {
      "Certificates": {
        "Path": "Certificates\IS4.pfx",
        "Password": "xxxxxx"
      }
    }

    参考资料:

    https://www.cnblogs.com/edisonchou/p/identityserver4_foundation_and_quickstart_01.html

    https://www.cnblogs.com/dingshuanglei/p/10237710.html

    https://www.cnblogs.com/ycm-up/p/9810525.html

  • 相关阅读:
    [转]直接拿来用!最火的Android开源项目(一)
    [原] jQuery EasyUI 1.3.2 离线API、Demo
    [原]通过GitHub Pages建立个人站点(详细步骤)
    [转]直接拿来用!最火的Android开源项目(二)
    堆排序
    单例模式
    【面试题】:火车运煤问题
    Aptana Studio 2.0 对JQuery的智能提示的支持
    vs2005 常用快捷键(备忘)
    关于ASP.NET在IIS上的一些问题的解决办法
  • 原文地址:https://www.cnblogs.com/fengchao1000/p/10254903.html
Copyright © 2011-2022 走看看