diff --git a/CoreCms.Net.Auth/AuthorizationSetup.cs b/CoreCms.Net.Auth/AuthorizationSetup.cs index 450d9e58..9c85bb97 100644 --- a/CoreCms.Net.Auth/AuthorizationSetup.cs +++ b/CoreCms.Net.Auth/AuthorizationSetup.cs @@ -24,6 +24,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; +using NETCore.Encrypt; namespace CoreCms.Net.Auth { @@ -100,6 +101,19 @@ namespace CoreCms.Net.Auth o.TokenValidationParameters = tokenValidationParameters; o.Events = new JwtBearerEvents { + OnMessageReceived = context => + { + var token = context.Request.Headers["Authorization"].ObjectToString().Replace("Bearer ", ""); + if (string.IsNullOrEmpty(token)) + { + context.Response.Headers.Append("Token-Error-Token", "authorization decryption failure!"); + } + else + { + context.Token = EncryptProvider.AESDecrypt(token, AppSettingsConstVars.JwtConfigSecretKey); + } + return Task.CompletedTask; + }, OnChallenge = context => { context.Response.Headers.Append("Token-Error", context.ErrorDescription); @@ -108,7 +122,19 @@ namespace CoreCms.Net.Auth OnAuthenticationFailed = context => { var token = context.Request.Headers["Authorization"].ObjectToString().Replace("Bearer ", ""); - var jwtToken = (new JwtSecurityTokenHandler()).ReadJwtToken(token); + if (string.IsNullOrEmpty(token)) + { + context.Response.Headers.Append("Token-Error-Token", "token is wrong!"); + } + + //进行aes解密 + var decodeToken = EncryptProvider.AESDecrypt(token, AppSettingsConstVars.JwtConfigSecretKey); + if (string.IsNullOrEmpty(decodeToken)) + { + context.Response.Headers.Append("Token-Error-Token", "token decryption failure!"); + } + + var jwtToken = (new JwtSecurityTokenHandler()).ReadJwtToken(decodeToken); if (jwtToken.Issuer != issuer) { @@ -205,6 +231,19 @@ namespace CoreCms.Net.Auth o.TokenValidationParameters = tokenValidationParameters; o.Events = new JwtBearerEvents { + OnMessageReceived = context => + { + var token = context.Request.Headers["Authorization"].ObjectToString().Replace("Bearer ", ""); + if (string.IsNullOrEmpty(token)) + { + context.Response.Headers.Append("Token-Error-Token", "authorization decryption failure!"); + } + else + { + context.Token = EncryptProvider.AESDecrypt(token, AppSettingsConstVars.JwtConfigSecretKey); + } + return Task.CompletedTask; + }, OnChallenge = context => { context.Response.Headers.Append("Token-Error", context.ErrorDescription); @@ -213,7 +252,19 @@ namespace CoreCms.Net.Auth OnAuthenticationFailed = context => { var token = context.Request.Headers["Authorization"].ObjectToString().Replace("Bearer ", ""); - var jwtToken = (new JwtSecurityTokenHandler()).ReadJwtToken(token); + if (string.IsNullOrEmpty(token)) + { + context.Response.Headers.Append("Token-Error-Token", "token is wrong!"); + } + + //进行aes解密 + var decodeToken = EncryptProvider.AESDecrypt(token, AppSettingsConstVars.JwtConfigSecretKey); + if (string.IsNullOrEmpty(decodeToken)) + { + context.Response.Headers.Append("Token-Error-Token", "token decryption failure!"); + } + + var jwtToken = (new JwtSecurityTokenHandler()).ReadJwtToken(decodeToken); if (jwtToken.Issuer != issuer) { diff --git a/CoreCms.Net.Auth/CoreCms.Net.Auth.csproj b/CoreCms.Net.Auth/CoreCms.Net.Auth.csproj index e46e2544..f3302e26 100644 --- a/CoreCms.Net.Auth/CoreCms.Net.Auth.csproj +++ b/CoreCms.Net.Auth/CoreCms.Net.Auth.csproj @@ -7,6 +7,7 @@ + diff --git a/CoreCms.Net.Auth/Policys/JwtToken.cs b/CoreCms.Net.Auth/Policys/JwtToken.cs index a8c53801..80d7decc 100644 --- a/CoreCms.Net.Auth/Policys/JwtToken.cs +++ b/CoreCms.Net.Auth/Policys/JwtToken.cs @@ -19,7 +19,7 @@ namespace CoreCms.Net.Auth.Policys /// /// JWTToken生成类 /// - public class JwtToken + public static class JwtToken { /// /// 获取基于JWT的Token @@ -27,7 +27,7 @@ namespace CoreCms.Net.Auth.Policys /// 需要在登陆的时候配置 /// 在startup中定义的参数 /// - public static dynamic BuildJwtToken(Claim[] claims, PermissionRequirement permissionRequirement) + public static JwtTokenResponseJson BuildJwtToken(Claim[] claims, PermissionRequirement permissionRequirement) { var now = DateTime.Now; // 实例化JwtSecurityToken diff --git a/CoreCms.Net.Services/User/CoreCmsUserServices.cs b/CoreCms.Net.Services/User/CoreCmsUserServices.cs index f12cb8ba..4333855a 100644 --- a/CoreCms.Net.Services/User/CoreCmsUserServices.cs +++ b/CoreCms.Net.Services/User/CoreCmsUserServices.cs @@ -31,6 +31,7 @@ using CoreCms.Net.Utility.Helper; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using NETCore.Encrypt; using SqlSugar; using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types; @@ -62,6 +63,7 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic private readonly ICoreCmsPromotionServices _coreCmsPromotionServices; private readonly ICoreCmsCouponServices _coreCmsCouponServices; + private readonly ICoreCmsAliPayUserInfoServices _aliPayUserInfoServices; public CoreCmsUserServices(IUnitOfWork unitOfWork @@ -72,7 +74,7 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic ICoreCmsUserWeChatInfoServices userWeChatInfoServices, ICoreCmsUserGradeServices userGradeServices, PermissionRequirement permissionRequirement, IHttpContextAccessor httpContextAccessor, ICoreCmsUserLogServices userLogServices, IServiceProvider serviceProvider, - ICoreCmsBillPaymentsServices billPaymentsServices, ICoreCmsDistributionGradeRepository distributionGradeRepository, ICoreCmsDistributionRepository distributionRepository, ICoreCmsPromotionServices coreCmsPromotionServices, ICoreCmsCouponServices coreCmsCouponServices) + ICoreCmsBillPaymentsServices billPaymentsServices, ICoreCmsDistributionGradeRepository distributionGradeRepository, ICoreCmsDistributionRepository distributionRepository, ICoreCmsPromotionServices coreCmsPromotionServices, ICoreCmsCouponServices coreCmsCouponServices, ICoreCmsAliPayUserInfoServices aliPayUserInfoServices) { _dal = dal; BaseDal = dal; @@ -92,6 +94,7 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic _distributionRepository = distributionRepository; _coreCmsPromotionServices = coreCmsPromotionServices; _coreCmsCouponServices = coreCmsCouponServices; + _aliPayUserInfoServices = aliPayUserInfoServices; } @@ -568,7 +571,7 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic /// 手机短信验证码登陆,同时兼有手机短信注册的功能,还有第三方账户绑定的功能 /// /// 实体数据 - /// 登录方式(1普通,2短信,3微信小程序拉取手机号) + /// 登录方式(1普通,2短信,3微信小程序拉取手机号,4支付宝小程序拉取手机号) /// /// public async Task SmsLogin(FMComAccountCreate entity, @@ -621,10 +624,9 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic userInfo.createTime = DateTime.Now; //没有此用户,创建此用户 - if (!string.IsNullOrEmpty(entity.sessionAuthId)) + if (!string.IsNullOrEmpty(entity.sessionAuthId) && loginType == (int)GlobalEnumVars.LoginType.WeChatPhoneNumber) { - var wxUserInfo = - await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId); + var wxUserInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId); if (wxUserInfo != null) { if (string.IsNullOrEmpty(entity.avatar)) entity.avatar = wxUserInfo.avatar; @@ -633,6 +635,17 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic userInfo.userWx = wxUserInfo?.id ?? 0; } } + //else if (!string.IsNullOrEmpty(entity.sessionAuthId) && loginType == (int)GlobalEnumVars.LoginType.AliPhoneNumber) + //{ + // var aliUserInfo = await _aliPayUserInfoServices.QueryByClauseAsync(p => p.userId == entity.sessionAuthId || p.openId== entity.sessionAuthId); + // if (aliUserInfo != null) + // { + // //if (string.IsNullOrEmpty(entity.avatar)) entity.avatar = AliUserInfo.avatar; + // //if (string.IsNullOrEmpty(entity.nickname)) entity.nickname = wxUserInfo.nickName; + // //userInfo.sex = AliUserInfo?.gender ?? 3; + // userInfo.userWx = aliUserInfo?.id ?? 0; + // } + //} var allConfigs = await _settingServices.GetConfigDictionaries(); @@ -643,28 +656,17 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic } else { - var defaultImage = - CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopDefaultImage); + var defaultImage = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopDefaultImage); userInfo.avatarImage = defaultImage; } - userInfo.nickName = !string.IsNullOrEmpty(entity.nickname) - ? entity.nickname - : UserHelper.FormatMobile(entity.mobile); + userInfo.nickName = !string.IsNullOrEmpty(entity.nickname) ? entity.nickname : UserHelper.FormatMobile(entity.mobile); if (entity.invitecode > 0) { var pid = UserHelper.GetUserIdByShareCode(entity.invitecode); var pInfo = await _dal.QueryByClauseAsync(p => p.id == pid); - if (pInfo != null) - { - userInfo.parentId = pid; - } - //else - //{ - // jm.msg = GlobalErrorCodeVars.Code10014; - // return jm; - //} + userInfo.parentId = pInfo != null ? pid : 0; } if (!string.IsNullOrEmpty(entity.password)) @@ -675,7 +677,6 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic jm.msg = GlobalErrorCodeVars.Code11009; return jm; } - userInfo.passWord = CommonHelper.EnPassword(entity.password, userInfo.createTime); } else @@ -688,22 +689,14 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic userInfo.grade = userGradeInfo?.id ?? 0; var userId = await _dal.InsertAsync(userInfo); - if (userId == 0) - { - jm.msg = GlobalErrorCodeVars.Code10000; - return jm; - } if (userId > 0) { if (entity.invitecode > 0 && userInfo.parentId > 0) { - var inviterUserIntegral = CommonHelper - .GetConfigDictionary(allConfigs, SystemSettingConstVars.InviterUserIntegral) - .ObjectToInt(); //是否开启积分功能 + var inviterUserIntegral = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.InviterUserIntegral).ObjectToInt(); //是否开启积分功能 if (inviterUserIntegral > 0) - await _userPointLogServices.SetPoint(userInfo.parentId, inviterUserIntegral, - (int)GlobalEnumVars.UserPointSourceTypes.PointTypeInviterUser, "发展用户:" + userId + "赠送积分"); + await _userPointLogServices.SetPoint(userInfo.parentId, inviterUserIntegral, (int)GlobalEnumVars.UserPointSourceTypes.PointTypeInviterUser, "发展用户:" + userId + "赠送积分"); } //是否默认注册分销商 @@ -727,6 +720,11 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic await _distributionRepository.InsertAsync(iData); } } + else if (userId == 0) + { + jm.msg = GlobalErrorCodeVars.Code10000; + return jm; + } userInfo = await _dal.QueryByIdAsync(userId); @@ -756,7 +754,7 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic } //判断是否是小程序里的微信登陆,如果是,就给他绑定微信账号 - if (!string.IsNullOrEmpty(entity.sessionAuthId)) + if (!string.IsNullOrEmpty(entity.sessionAuthId) && loginType == (int)GlobalEnumVars.LoginType.WeChatPhoneNumber) { var updateAsync = await _userWeChatInfoServices.UpdateAsync(p => new CoreCmsUserWeChatInfo() { userId = userInfo.id }, p => p.openid == entity.sessionAuthId); if (updateAsync) @@ -768,7 +766,6 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic //会导致微信数据报错() //2023-05-05 屏蔽此代码,是为了让公众号,小程序实现多openid指向同一个账号,但是也存在问题,就是多了之后的解绑问题,应该做个登录设备管理。 - //await _userWeChatInfoServices.UpdateAsync(p => new CoreCmsUserWeChatInfo() { userId = 0 }, p => p.openid != entity.sessionAuthId && p.userId == userInfo.id); } @@ -780,6 +777,10 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic } } + else if (!string.IsNullOrEmpty(entity.sessionAuthId) && loginType == (int)GlobalEnumVars.LoginType.AliPhoneNumber) + { + await _aliPayUserInfoServices.UpdateAsync(p => new CoreCmsAliPayUserInfo() { userInfoId = userInfo.id }, p => p.openId == entity.sessionAuthId || p.userId == entity.sessionAuthId); + } if (userInfo.status == (int)GlobalEnumVars.UserStatus.正常) { @@ -799,7 +800,13 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); - jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + + jm.data = auth; } else { @@ -875,7 +882,6 @@ public class CoreCmsUserServices : BaseServices, ICoreCmsUserServic #endregion - #region 当关闭分销和代理的时候走个人邀请好友分佣 /// diff --git a/CoreCms.Net.Web.Admin/Controllers/Com/LoginController.cs b/CoreCms.Net.Web.Admin/Controllers/Com/LoginController.cs index dcf1fd5e..ee5777cf 100644 --- a/CoreCms.Net.Web.Admin/Controllers/Com/LoginController.cs +++ b/CoreCms.Net.Web.Admin/Controllers/Com/LoginController.cs @@ -31,6 +31,7 @@ using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; +using NETCore.Encrypt; namespace CoreCms.Net.Web.Admin.Controllers { @@ -124,13 +125,16 @@ namespace CoreCms.Net.Web.Admin.Controllers var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); - var token = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); jm.code = 0; jm.msg = "认证成功"; jm.data = new { - token, + token = auth, loginUrl = "Panel.html" }; diff --git a/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj b/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj index 5550a5bd..08d2adc8 100644 --- a/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj +++ b/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj @@ -61,6 +61,7 @@ + diff --git a/CoreCms.Net.Web.WebApi/Controllers/AliPayOAuth/AliPayAuthController.cs b/CoreCms.Net.Web.WebApi/Controllers/AliPayOAuth/AliPayAuthController.cs index 2b9719b7..93155e72 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/AliPayOAuth/AliPayAuthController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/AliPayOAuth/AliPayAuthController.cs @@ -16,6 +16,7 @@ using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.FromBody; using CoreCms.Net.Configuration; using static SKIT.FlurlHttpClient.Wechat.Api.Models.WeDataQueryBindListResponse.Types; +using NETCore.Encrypt; namespace CoreCms.Net.Web.WebApi.Controllers.AliPayOAuth { @@ -137,10 +138,16 @@ namespace CoreCms.Net.Web.WebApi.Controllers.AliPayOAuth //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + jm.status = true; jm.data = new { - auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement), + auth, user }; jm.otherData = result.UserId; diff --git a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs index fbd1ddff..bf9ab583 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs @@ -48,6 +48,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using NETCore.Encrypt; using Newtonsoft.Json; using Nito.AsyncEx; using NLog; @@ -252,10 +253,16 @@ namespace CoreCms.Net.Web.WebApi.Controllers //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + jm.status = true; jm.data = new { - auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement), + auth, user }; jm.otherData = response.OpenId; @@ -355,10 +362,16 @@ namespace CoreCms.Net.Web.WebApi.Controllers //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + jm.status = true; jm.data = new { - auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement), + auth, user }; jm.otherData = response.OpenId; @@ -468,8 +481,14 @@ namespace CoreCms.Net.Web.WebApi.Controllers //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + jm.status = true; - jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + jm.data = auth; //录入登录日志 var log = new CoreCmsUserLog(); @@ -801,9 +820,15 @@ namespace CoreCms.Net.Web.WebApi.Controllers //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); + + //返回处理结果集 + var auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //对token进行非对称加密 + auth.token = EncryptProvider.AESEncrypt(auth.token, AppSettingsConstVars.JwtConfigSecretKey); + jm.status = true; jm.msg = "注册成功"; - jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + jm.data = auth; //录入登录日志 var log = new CoreCmsUserLog(); log.userId = id; diff --git a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj index 12de47c6..9e1b0105 100644 --- a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj +++ b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj @@ -67,6 +67,7 @@ + diff --git a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml index b15e5c24..f89c4cfe 100644 --- a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml +++ b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml @@ -1081,7 +1081,7 @@ 用户操作事件 - + 构造函数 @@ -1107,7 +1107,7 @@ - + 手机短信验证码登陆,同时兼有手机短信注册的功能,还有第三方账户绑定的功能 @@ -1121,7 +1121,14 @@ - + + + 支付宝小程序授权拉取手机号码 + + + + + 用户短信注册并返回jwt token(弃用)