Skip to content
.NET 开发者指北.NET 开发者指北
CMS
.NET指北
FreeKit
Docker
关于
博客
github icon
    • Lin CMS By .NET
      • 后端准备
        • Server 端必备环境
          • 获取工程项目
            • 数据库配置
              • 数据迁移
                • visual studio 2022 运行项目
                  • Ids4 服务端
                    • 部署
                      • 部署前准备
                        • 使用 OpenSSL 生成证书
                          • (注:在生成的过程中会让我们输入 Export Password)
                          • 参考
                          • 后端快速上手
                            • 相关技术
                              • 前端准备
                                • 开源介绍
                                  • 功能模块的设计
                                    • 产品设计
                                      • GitHub Actions
                                        • LinCms如何切换成SQL server

                                        后端准备

                                        calendar icon2020年2月28日timer icon大约 4 分钟word icon约 1073 字

                                        此页内容
                                        • Server 端必备环境
                                        • 获取工程项目
                                        • 数据库配置
                                        • 数据迁移
                                        • visual studio 2022 运行项目
                                        • Ids4 服务端
                                        • 部署
                                        • 部署前准备
                                          • 使用 OpenSSL 生成证书
                                          • (注:在生成的过程中会让我们输入 Export Password)
                                        • 参考

                                        # 后端准备

                                        # Server 端必备环境

                                        • 安装软件开发包 .NET SDK 6.0open in new window
                                        • 安装开发工具 Visual Studio 2022open in new window 或 Rideropen in new window
                                        • 安装 MySQL(version 5.7+)及以上版本
                                        • Redis 4.0.14.2 for Windows https://github.com/tporadowski/redis/releasesopen in new window
                                        • Rabbitmq(非必要),默认基于Memory级别

                                        # 获取工程项目

                                        git clone https://github.com/luoyunchong/lin-cms-dotnetcore.git
                                        
                                        1

                                        # 数据库配置

                                        文件位置src/LinCms.Web/appsettings.json,当数据库中存储表情包是,Charset为utf8mb4

                                        请务必根据自己的实际情况修改此配置项,DefaultDB为 0 时,代表使用MySQL,DefaultDB为 1 时,代表使用SqlServer,以此类推。

                                         "ConnectionStrings": {
                                            "DefaultDB": "0",
                                            "DataType": {
                                              "MySql": 0,
                                              "SqlServer": 1,
                                              "PostgreSQL": 2,
                                              "Oracle": 3,
                                              "Sqlite": 4
                                            },
                                            "MySql": "Data Source=localhost;Port=3306;User ID=root;Password=root;Initial Catalog=lincms;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20",
                                            "SqlServer": "Data Source=.;User ID=sa;Password=123456;Integrated Security=True;Initial Catalog=LinCMS;Pooling=true;Min Pool Size=1",
                                            "PostgreSQL": "Host=localhost;Port=5432;Username=postgres;Password=123456; Database=lincms;Pooling=true;Minimum Pool Size=1",
                                            "Oracle": "user id=root;password=root; data source=//127.0.0.1:1521/ORCL;Pooling=true;Min Pool Size=1",
                                            "Sqlite": "Data Source=|DataDirectory|\\lincms.db; Attachs=lincms.db; Pooling=true;Min Pool Size=1",
                                            "CsRedis": "127.0.0.1:6379,password=,defaultDatabase=0"
                                          },
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6
                                        7
                                        8
                                        9
                                        10
                                        11
                                        12
                                        13
                                        14
                                        15
                                        16

                                        LinCms.IdentityServer4 项目不是必须的,根据LinCms.Web中的appsettings.json配置 项 Service.IdentityServer4设置成了true,LinCms.Web项目中的登录接口才会访问Ids4项目,

                                        如需运行,则修改数据库配置项

                                        identityserver4/LinCms.IdentityServer4/appsettings.json 数据库配置、同LinCms.Web中的配置项相同

                                          "ConnectionStrings": {
                                            "DefaultDB": "0",
                                            "DataType": {
                                              "MySql": 0,
                                              "SqlServer": 1,
                                              "PostgreSQL": 2,
                                              "Oracle": 3,
                                              "Sqlite": 4
                                            },
                                            "MySql": "Data Source=localhost;Port=3308;User ID=root;Password=root;Initial Catalog=lincms;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20",
                                          },
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6
                                        7
                                        8
                                        9
                                        10
                                        11

                                        其中 MariaDB(看做 MySql),通过Serilog记录日志,需要配置相应的链接串。

                                        {
                                            "Name": "MariaDB",
                                            "Args": {
                                                "connectionString": "Data Source=localhost;Port=3306;User ID=root;Password=root;Initial Catalog=lincms;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20",
                                            }
                                        }
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6

                                        # 数据迁移

                                        该项目使用FreeSqlopen in new window,会自动根据配置项创建数据库,默认自动迁移数据表结构,初始化种子数据

                                        默认会创建用户admin,密码123qwe

                                        # visual studio 2022 运行项目

                                        双击 LinCms.sln,使用 vs2022 打开项目。右键解决方案,点击生成解决方案。

                                        # Ids4 服务端

                                        默认情况下 Service.IdentityServer4设置成了false,即LinCms.IdentityServer4不需要运行,我们就只需要运行LinCms.Web项目即可

                                          "Service": {
                                            "IdentityServer4": false,
                                            "Name": "LinCms.Web",
                                            "GrantType": "password",
                                            "ClientId": "lin-cms-dotnetcore-client-id",
                                            "ClientSecret": "lin-cms-dotnetcore-client-secrets",
                                            "Authority": "https://localhost:5003",
                                            "UseHttps": true
                                          },
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6
                                        7
                                        8
                                        9

                                        如果Service.IdentityServer4设置成了true

                                        由于将 identityserver4 单独拆成了一个项目,所以需要同时启动二个项目,右键解决方案,属性。,选择多个启动项目,勾选二个项目同时启动。如下图所示。

                                        这时候会打开二个网页 https://localhost:5001/swagger/index.htmlopen in new window,即可看到 swagger 页面。

                                        会打开浏览器,访问https://localhosst:5001/swagger/index.htmlopen in new window,会看到 swagger 的文档。 访问https://localhosst:5003/swagger/index.htmlopen in new window 是 ids4 的接口文档,什么也看不到。

                                        LinCms.Web 运行效果:

                                        # 部署

                                        • 部署至 Linux(Ubuntu16.06)open in new window
                                        • 部署至 Linux(Ubuntu16.06)下的 Dockeropen in new window

                                        # 部署前准备

                                        因为该项目基于 IdentityServer4,实现的授权认证服务,

                                        开发阶段使用AddDeveloperSigningCredential()方法即可完成签名认证,但是在生产环境,我们必须使用AddSigningCredential()方法并且使用 OpenSSL 生成自己的签名证书

                                        Startup.cs

                                                    services.AddIdentityServer()
                                        #if  DEBUG
                                                        .AddDeveloperSigningCredential()
                                        #endif
                                        #if !DEBUG
                                                        .AddSigningCredential(new X509Certificate2(Path.Combine(AppContext.BaseDirectory,
                                                                Configuration["Certificates:Path"]),
                                                            Configuration["Certificates:Password"]))
                                        #endif
                                                        .AddInMemoryIdentityResources(InMemoryConfiguration.GetIdentityResources())
                                                        .AddInMemoryApiResources(InMemoryConfiguration.GetApis())
                                                        .AddInMemoryClients(InMemoryConfiguration.GetClients())
                                                        .AddProfileService<LinCmsProfileService>()
                                                        .AddResourceOwnerValidator<LinCmsResourceOwnerPasswordValidator>();
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6
                                        7
                                        8
                                        9
                                        10
                                        11
                                        12
                                        13
                                        14

                                        appsettings.Production.json

                                        {
                                            "Certificates":
                                            {
                                                "Path":"ids4.pfx",
                                                "Password":"123qwe"
                                            }
                                        }
                                        
                                        1
                                        2
                                        3
                                        4
                                        5
                                        6
                                        7

                                        # 使用 OpenSSL 生成证书

                                        官网下载并安装 OpenSSL OpenSSL 官网open in new window

                                        下载 Win64 OpenSSL v1.1.1b 版本

                                        在 OpenSSL 的 bin 文件夹,以管理员身份打开 CMD 执行以下命令:

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

                                        下面将生成的证书和 Key 封装成一个文件,以便 IdentityServer 可以使用它们去正确地签名 tokens

                                        openssl pkcs12 -export -in ids4.cer -inkey ids4.key -out ids4.pfx
                                        
                                        1

                                        # (注:在生成的过程中会让我们输入 Export Password)

                                        这个 密码与 appsettings.Production.json 配置项相同。

                                        发布时,把 ids4.pfx,放到项目根目录

                                        # 参考

                                        • IdentityServer4 之 JWT 签名(RSA 加密证书)及验签open in new window
                                        edit icon在 GitHub 上编辑此页open in new window
                                        上次编辑于: 2022/10/24 14:14:31
                                        贡献者: luoyunchong,igeekfan
                                        上一页
                                        Lin CMS By .NET
                                        下一页
                                        后端快速上手
                                        MIT Licensed | Copyright © 2021-present luoyunchong
                                        苏ICP备16046457号-1

                                        该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                                        详情