diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs index 2b89b4c..3fc24e6 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs @@ -86,7 +86,7 @@ namespace ZR.Admin.WebApi.Controllers.System List permissions = permissionService.GetMenuPermission(user); LoginUser loginUser = new(user, roles, permissions); - CacheHelper.SetCache(GlobalConstant.UserPermKEY + user.UserId, permissions); + CacheService.SetUserPerms(GlobalConstant.UserPermKEY + user.UserId, permissions); return SUCCESS(JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), jwtSettings.JwtSettings)); } @@ -106,7 +106,7 @@ namespace ZR.Admin.WebApi.Controllers.System var userid = HttpContext.GetUId(); var name = HttpContext.GetName(); - CacheHelper.Remove(GlobalConstant.UserPermKEY + userid); + CacheService.RemoveUserPerms(GlobalConstant.UserPermKEY + userid); return SUCCESS(new { name , id = userid }); } diff --git a/ZR.Admin.WebApi/Extensions/EntityExtension.cs b/ZR.Admin.WebApi/Extensions/EntityExtension.cs index fe5c4a8..7b4c11f 100644 --- a/ZR.Admin.WebApi/Extensions/EntityExtension.cs +++ b/ZR.Admin.WebApi/Extensions/EntityExtension.cs @@ -10,14 +10,6 @@ namespace ZR.Admin.WebApi.Extensions { var types = source.GetType(); - //var worker = new IdWorker(1, 1); - //if (types.GetProperty("ID") != null) - //{ - // long id = worker.NextId(); - - // types.GetProperty("ID").SetValue(source, id.ToString(), null); - //} - if (types.GetProperty("CreateTime") != null) { types.GetProperty("CreateTime").SetValue(source, DateTime.Now, null); @@ -30,7 +22,6 @@ namespace ZR.Admin.WebApi.Extensions { types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null); } - if (types.GetProperty("Create_by") != null && context != null) { types.GetProperty("Create_by").SetValue(source, context.GetName(), null); @@ -54,10 +45,6 @@ namespace ZR.Admin.WebApi.Extensions { types.GetProperty("Update_time").SetValue(source, DateTime.Now, null); } - //if (types.GetProperty("UpdateID") != null) - //{ - // types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null); - //} if (types.GetProperty("UpdateBy") != null) { diff --git a/ZR.Admin.WebApi/Extensions/HttpContextExtension.cs b/ZR.Admin.WebApi/Extensions/HttpContextExtension.cs index 2103a91..dea415d 100644 --- a/ZR.Admin.WebApi/Extensions/HttpContextExtension.cs +++ b/ZR.Admin.WebApi/Extensions/HttpContextExtension.cs @@ -1,9 +1,6 @@ -using Infrastructure; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Http; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; @@ -130,26 +127,6 @@ namespace ZR.Admin.WebApi.Extensions return context != null ? context.Request.Path.Value : ""; } - private static void WhiteCookie(HttpContext context, List claims) - { - //2.创建声明主题 指定认证方式 这里使用cookie - var claimsIdentity = new ClaimsIdentity(claims, "Login"); - - Task.Run(async () => - { - await context.SignInAsync( - JwtBearerDefaults.AuthenticationScheme,//这里要注意的是HttpContext.SignInAsync(AuthenticationType,…) 所设置的Scheme一定要与前面的配置一样,这样对应的登录授权才会生效。 - new ClaimsPrincipal(claimsIdentity), - new AuthenticationProperties() - { - IsPersistent = true, - AllowRefresh = true, - ExpiresUtc = DateTimeOffset.Now.AddDays(1),//有效时间 - }); - }).Wait(); - } - - /// /// 设置请求参数 /// diff --git a/ZR.Admin.WebApi/Framework/CookieUtil.cs b/ZR.Admin.WebApi/Framework/CookieUtil.cs index eeb7e26..82081fd 100644 --- a/ZR.Admin.WebApi/Framework/CookieUtil.cs +++ b/ZR.Admin.WebApi/Framework/CookieUtil.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; @@ -11,7 +12,23 @@ namespace ZR.Admin.WebApi.Framework { public class CookieUtil { + public static void WhiteCookie(HttpContext context, List claims) + { + //2.创建声明主题 指定认证方式 这里使用cookie + var claimsIdentity = new ClaimsIdentity(claims, "Login"); - + Task.Run(async () => + { + await context.SignInAsync( + JwtBearerDefaults.AuthenticationScheme,//这里要注意的是HttpContext.SignInAsync(AuthenticationType,…) 所设置的Scheme一定要与前面的配置一样,这样对应的登录授权才会生效。 + new ClaimsPrincipal(claimsIdentity), + new AuthenticationProperties() + { + IsPersistent = true, + AllowRefresh = true, + ExpiresUtc = DateTimeOffset.Now.AddDays(1),//有效时间 + }); + }).Wait(); + } } } diff --git a/ZR.Admin.WebApi/Framework/JwtUtil.cs b/ZR.Admin.WebApi/Framework/JwtUtil.cs index 3a52795..590b615 100644 --- a/ZR.Admin.WebApi/Framework/JwtUtil.cs +++ b/ZR.Admin.WebApi/Framework/JwtUtil.cs @@ -10,8 +10,8 @@ using System.Linq; using System.Security.Claims; using System.Text; using ZR.Admin.WebApi.Extensions; -using ZR.Common; using ZR.Model.System; +using ZR.Service.System; namespace ZR.Admin.WebApi.Framework { @@ -131,7 +131,7 @@ namespace ZR.Admin.WebApi.Framework { var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData).Value; var loginUser = JsonConvert.DeserializeObject(userData); - var permissions = (List)CacheHelper.GetCache(GlobalConstant.UserPermKEY + loginUser?.UserId); + var permissions = CacheService.GetUserPerms(GlobalConstant.UserPermKEY + loginUser?.UserId); if (loginUser?.UserName == "admin") { permissions = new List() { GlobalConstant.AdminPerm }; diff --git a/ZR.Service/System/CacheService.cs b/ZR.Service/System/CacheService.cs new file mode 100644 index 0000000..a18a0bd --- /dev/null +++ b/ZR.Service/System/CacheService.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZR.Common; + +namespace ZR.Service.System +{ + public class CacheService + { + #region 用户权限 缓存 + public static List GetUserPerms(string key) + { + return (List)CacheHelper.GetCache(key); + } + + public static void SetUserPerms(string key, object data) + { + CacheHelper.SetCache(key, data); + } + public static void RemoveUserPerms(string key) + { + CacheHelper.Remove(key); + } + #endregion + } +}