后端准备
# 后端准备
# Server 端必备环境
- 安装软件开发包 .NET SDK 6.0 (opens new window)
- 安装开发工具 Visual Studio 2022 (opens new window) 或 Rider (opens new window)
- 安装MySQL(version 5.7+)
- Redis 4.0.14.2 for Windows https://github.com/tporadowski/redis/releases (opens new window)
# 获取工程项目
git clone https://github.com/luoyunchong/lin-cms-dotnetcore.git
# 数据库配置
文件位置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": null,
"Sqlite": "Data Source=|DataDirectory|\\lincms.db; Attachs=lincms.db; Pooling=true;Min Pool Size=1",
"CsRedis": "127.0.0.1:6379,password=,defaultDatabase=0"
},
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LinCms.IdentityServer4
项目不是必须的,需要需要运行,需要修改数据库配置项
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",
"SqlServer": "Data Source=.;User ID=sa;Password=123456;Integrated Security=True;Initial Catalog=LinCMS;Pooling=true;Min Pool Size=1",
"Sqlite": "Data Source=|DataDirectory|\\lincms.db; Attachs=lincms.db; Pooling=true;Min Pool Size=1"
},
2
3
4
5
6
7
8
9
10
11
12
13
其中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",
}
2
3
4
5
# 数据迁移
该项目使用FreeSql (opens new window),默认自动迁移数据表结构,会自动根据配置项创建数据库,初始化种子数据
默认会创建用户admin
,密码123qwe
# visual studio 2022运行项目
双击lin-cms-dotnetcore.sln,使用vs2022打开项目。右键解决方案,点击生成解决方案。
由于将identityserver4单独拆成了一个项目,所以需要同时启动二个项目,右键解决方案,属性。,选择多个启动项目,勾选二个项目同时启动。如下图所示。
这时候会打开二个网页 https://localhost:5001/swagger/index.html,即可看到swagger页面。
会打开浏览器,访问https://localhosst:5001/swagger/index.html (opens new window),会看到swagger的文档。 访问https://localhosst:5003/swagger/index.html (opens new window) 是ids4的接口文档,什么也看不到。
LinCms.Web运行效果:
# 部署
# 部署前准备
因为该项目基于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>();
2
3
4
5
6
7
8
9
10
11
12
13
14
appsettings.Production.json
{
"Certificates":
{
"Path":"ids4.pfx",
"Password":"123qwe"
}
}
2
3
4
5
6
7
# 使用OpenSSL生成证书
官网下载并安装OpenSSL OpenSSL官网 (opens 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
下面将生成的证书和Key封装成一个文件,以便IdentityServer可以使用它们去正确地签名tokens
openssl pkcs12 -export -in ids4.cer -inkey ids4.key -out ids4.pfx
# (注:在生成的过程中会让我们输入Export Password)
这个 密码与appsettings.Production.json配置项相同。
发布时,把ids4.pfx,放到项目根目录