Skip to content

Commit

Permalink
添加数据保护,优先在外部支持Redis持久化,这里默认使用数据库持久化
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 6, 2023
1 parent 63c4e0a commit dbfc22f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions CubeDemoNC/CubeDemoNC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.11" />
<PackageReference Include="NewLife.Redis.Extensions" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Stardust.Extensions" Version="2.9.2023.1001" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions CubeDemoNC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CubeDemoNC;
public class Program
{
public static void Main(string[] args)
{
{
Environment.SetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1");

XTrace.UseConsole();
Expand All @@ -18,13 +18,13 @@ public static void Main(string[] args)
do
{
app.Start(CreateHostBuilder(args).Build());
} while ( app.Restarting);
} while (app.Restarting);
}

//public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
// WebHost.CreateDefaultBuilder(args)
// .UseStartup<Startup>();


public static IHostBuilder CreateHostBuilder(String[] args)
{
Expand Down
24 changes: 24 additions & 0 deletions CubeDemoNC/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
using NewLife;
using NewLife.Caching.Services;
using NewLife.Caching;
using NewLife.Cube;
using NewLife.Cube.AdminLTE;
using NewLife.Cube.Extensions;
using NewLife.Cube.WebMiddleware;
using CubeSetting = NewLife.Cube.CubeSetting;
using NewLife.Cube.Services;
using NewLife.Redis.Extensions;
using Microsoft.Extensions.DependencyInjection;
using NewLife.Log;
using Microsoft.AspNetCore.DataProtection;

namespace CubeDemoNC;

Expand All @@ -21,12 +28,29 @@ public void ConfigureServices(IServiceCollection services)
var star = services.AddStardust(null);
TracerMiddleware.Tracer = star?.Tracer;

// 分布式服务,使用配置中心RedisCache配置
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

var config = star.GetConfig();
var cacheConn = config["RedisCache"];
if (!cacheConn.IsNullOrEmpty())
{
var redis = new FullRedis { Log = XTrace.Log, Tracer = star.Tracer };
redis.Init(cacheConn);
services.AddSingleton(redis);
}

// 启用接口响应压缩
services.AddResponseCompression();

services.AddControllersWithViews();
services.AddCube();
//services.AddBlazor();

if (!cacheConn.IsNullOrEmpty())
{
services.AddDataProtection().PersistKeysToRedis();
}
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
1 change: 1 addition & 0 deletions CubeDemoNC/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
"RedisCache": "server=127.0.0.1;password=;db=2",
"ConnectionStrings": {
"Membership": "Data Source=..\\Data\\Membership.db;provider=sqlite",
"Log": "Data Source=..\\Data\\Log.db;provider=sqlite",
Expand Down
12 changes: 6 additions & 6 deletions NewLife.Cube/CubeService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.IO;
using System.Reflection;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.WebEncoders;
using Microsoft.Net.Http.Headers;
Expand All @@ -15,7 +12,6 @@
using NewLife.Cube.WebMiddleware;
using NewLife.IP;
using NewLife.Log;
using NewLife.Reflection;
using NewLife.Serialization;
using NewLife.Web;
using Stardust;
Expand Down Expand Up @@ -97,7 +93,11 @@ public static IServiceCollection AddCube(this IServiceCollection services)
// 添加管理提供者
services.AddManageProvider();

// 添加数据保护
// 添加数据保护,优先在外部支持Redis持久化,这里默认使用数据库持久化
//if (services.Any(e => e.ServiceType == typeof(FullRedis) || e.ServiceType == typeof(ICacheProvider) && e.ImplementationType == typeof(RedisCacheProvider)))
// services.AddDataProtection().PersistKeysToRedis();
//else
// services.AddDataProtection().PersistKeysToDb();
services.AddDataProtection()
.PersistKeysToDb();

Expand Down
1 change: 1 addition & 0 deletions NewLife.Cube/NewLife.Cube.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
<PackageReference Include="NewLife.IP" Version="2.1.2023.704-beta0221" />
<PackageReference Include="NewLife.Redis.Extensions" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1001" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1001" />
</ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions NewLife.CubeNC/CubeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.WebEncoders;
using Microsoft.Net.Http.Headers;
using NewLife.Common;
using NewLife.Cube.Entity;
using NewLife.Cube.Extensions;
using NewLife.Cube.Modules;
using NewLife.Cube.Services;
Expand Down Expand Up @@ -79,7 +77,6 @@ public static IServiceCollection AddCube(this IServiceCollection services)
// options.MinimumSameSitePolicy = SameSiteMode.None;
//});


// 添加Session会话支持
services.AddSession();

Expand Down Expand Up @@ -132,7 +129,11 @@ public static IServiceCollection AddCube(this IServiceCollection services)
// 添加管理提供者
services.AddManageProvider();

// 添加数据保护
// 添加数据保护,优先在外部支持Redis持久化,这里默认使用数据库持久化
//if (services.Any(e => e.ServiceType == typeof(FullRedis) || e.ServiceType == typeof(ICacheProvider) && e.ImplementationType == typeof(RedisCacheProvider)))
// services.AddDataProtection().PersistKeysToRedis();
//else
// services.AddDataProtection().PersistKeysToDb();
services.AddDataProtection()
.PersistKeysToDb();

Expand Down
3 changes: 3 additions & 0 deletions NewLife.CubeNC/Services/DbXmlRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Xml.Linq;
using Microsoft.AspNetCore.DataProtection.Repositories;
using NewLife.Log;
using NewLife.Serialization;
using NewLife.Threading;
using XCode.Membership;
Expand All @@ -18,6 +19,8 @@ public DbXmlRepository(String key)
{
if (key.IsNullOrEmpty()) throw new ArgumentNullException(nameof(key));

XTrace.WriteLine("DataProtection使用数据库持久化密钥,Key={0}", key);

_key = key;

_timer = new TimerX(TrimExpired, null, 1_000, 3600_000) { Async = true };
Expand Down
9 changes: 3 additions & 6 deletions NewLife.CubeNC/Views/Shared/_List_Data_Item.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ else
}
else
{
var val = value;
var val = value;
if (!item.MapField.IsNullOrEmpty() && val + "" != "")
{
if (item.MapProvider != null)
Expand All @@ -179,7 +179,7 @@ else
val = entity[item.MapField];
//从菜单查找相关
var menu = Menu.FindForName(@name);
if (menu!=null)
if (menu != null)
{
//获取控制器名称,拼接全路径
var fullPath = $"{this.Context.Request.Scheme}://{this.Context.Request.Host}/{menu.ParentNodeName}/{@name}?{@key}={@val}";
Expand Down Expand Up @@ -208,15 +208,12 @@ else
{
if (!item.ItemType.IsNullOrEmpty() && item.ItemType.EqualIgnoreCase("file-WebUploader"))
{

<td class="text-center">@Html.Partial("_View_ImgView",value)</td>

<td class="text-center">@await Html.PartialAsync("_View_ImgView",value)</td>
}
else
{
<td>@value</td>
}

}
}
break;
Expand Down

0 comments on commit dbfc22f

Please sign in to comment.