From 8e9caee653dc29d61a10919f216350013db6346b Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 11 Sep 2025 13:15:48 +0800 Subject: [PATCH] make --- CoreCms.Net.Auth/AuthorizationSetup.cs | 6 ++- .../User/CoreCmsUserServices.cs | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CoreCms.Net.Auth/AuthorizationSetup.cs b/CoreCms.Net.Auth/AuthorizationSetup.cs index a62029f..5e1f43a 100644 --- a/CoreCms.Net.Auth/AuthorizationSetup.cs +++ b/CoreCms.Net.Auth/AuthorizationSetup.cs @@ -15,9 +15,11 @@ using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; + using CoreCms.Net.Auth.Policys; using CoreCms.Net.Configuration; using CoreCms.Net.Utility.Extensions; + using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; @@ -167,7 +169,7 @@ namespace CoreCms.Net.Auth issuer,//发行人 audience,//听众 signingCredentials,//签名凭据 - expiration: TimeSpan.FromSeconds(60 * 60 * 24)//接口的过期时间 + expiration: TimeSpan.FromMinutes(60 * 2)//接口的过期时间 ); #endregion @@ -188,7 +190,7 @@ namespace CoreCms.Net.Auth ValidateAudience = true, //是否验证Audience ValidAudience = audience,//订阅人 ValidateLifetime = true, //是否验证失效时间 - ClockSkew = TimeSpan.FromSeconds(60), + ClockSkew = TimeSpan.FromMinutes(60 * 1), RequireExpirationTime = true, }; diff --git a/CoreCms.Net.Services/User/CoreCmsUserServices.cs b/CoreCms.Net.Services/User/CoreCmsUserServices.cs index d2b7828..cad59d7 100644 --- a/CoreCms.Net.Services/User/CoreCmsUserServices.cs +++ b/CoreCms.Net.Services/User/CoreCmsUserServices.cs @@ -646,6 +646,49 @@ namespace CoreCms.Net.Services return jm; } + + public async Task AnonymousLogin(int user_id) + { + var jm = new WebApiCallBack(); + var userInfo = await _dal.QueryByClauseAsync(p => p.id == user_id); + if (userInfo != null) + { + + if (userInfo.status == (int)GlobalEnumVars.UserStatus.正常) + { + var claims = new List { + new Claim(ClaimTypes.Name, userInfo.nickName), + new Claim(JwtRegisteredClaimNames.Jti, userInfo.id.ToString()), + new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_permissionRequirement.Expiration.TotalSeconds).ToString()) }; + //用户标识 + var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); + identity.AddClaims(claims); + jm.status = true; + jm.msg = "注册成功"; + jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement); + //录入登录日志 + var log = new CoreCmsUserLog(); + log.userId = userInfo.id; + log.state = (int)GlobalEnumVars.UserLogTypes.登录; + log.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? + _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1"; + log.createTime = DateTime.Now; + log.parameters = GlobalEnumVars.UserLogTypes.登录.ToString(); + await _userLogServices.InsertAsync(log); + } + else + { + jm.msg = GlobalErrorCodeVars.Code11022; + return jm; + } + + } + return new WebApiCallBack() + { + status = false, + msg = "用户不存在" + }; + } #endregion