diff --git a/Tiobon.Core.Common/UserManager/HttpContext.cs b/Tiobon.Core.Common/UserManager/HttpContext.cs new file mode 100644 index 00000000..22b9efa7 --- /dev/null +++ b/Tiobon.Core.Common/UserManager/HttpContext.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Http; + +namespace Tiobon.Core.Common +{ + public static class HttpUseContext + { + private static IHttpContextAccessor _accessor; + + public static Microsoft.AspNetCore.Http.HttpContext Current => _accessor.HttpContext; + + internal static void Configure(IHttpContextAccessor accessor) + { + _accessor = accessor; + } + } +} diff --git a/Tiobon.Core.Common/UserManager/StaticHttpContextExtensions.cs b/Tiobon.Core.Common/UserManager/StaticHttpContextExtensions.cs new file mode 100644 index 00000000..573b65cf --- /dev/null +++ b/Tiobon.Core.Common/UserManager/StaticHttpContextExtensions.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace Tiobon.Core.Common; +public static class StaticHttpContextExtensions +{ + //public static void AddHttpContextAccessor(this IServiceCollection services) + //{ + // services.AddSingleton(); + //} + + public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app) + { + var httpContextAccessor = app.ApplicationServices.GetRequiredService(); + HttpUseContext.Configure(httpContextAccessor); + return app; + } +} \ No newline at end of file diff --git a/Tiobon.Core.Common/UserManager/UserContext.cs b/Tiobon.Core.Common/UserManager/UserContext.cs new file mode 100644 index 00000000..d39f6962 --- /dev/null +++ b/Tiobon.Core.Common/UserManager/UserContext.cs @@ -0,0 +1,102 @@ +namespace Tiobon.Core.Common.UserManager +{ + public class UserContext + { + /// + /// 为了尽量减少redis或Memory读取,保证执行效率,将UserContext注入到DI, + /// 每个UserContext的属性至多读取一次redis或Memory缓存从而提高查询效率 + /// + public static UserContext Current + { + get + { + //try + //{ + // return Context.RequestServices.GetService(typeof(UserContext)) as UserContext; + //} + //catch (Exception) + //{ + // return new UserContext(); + //} + return new UserContext(); + } + } + + private static Microsoft.AspNetCore.Http.HttpContext Context + { + get + { + return HttpUseContext.Current; + } + } + + /// + /// 用户ID + /// + public long? User_Id + { + get + { + try + { + //string aa = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); + string userId = Context?.User?.Identity?.Name; + return string.IsNullOrEmpty(userId) ? null : long.Parse(userId); + } + catch (Exception) + { + return null; //匿名访问 + } + } + } + + //private SmUser _userInfo { get; set; } + + //public SmUser UserInfo + //{ + // get + // { + // if (_userInfo != null) + // { + // return _userInfo; + // } + // return GetUserInfo(User_Id); + // } + //} + + //public SmUser GetUserInfo(Guid? userId) + //{ + // if (_userInfo != null) return _userInfo; + // if (userId is null) + // { + // _userInfo = new SmUser(); + // return _userInfo; + // } + // _userInfo = Redis.Get(userId.ToString()); + // if (_userInfo == null) + // { + // string sql = "SELECT A.* FROM SmUsers A WHERE A.IsDeleted='false' AND ID='{0}'"; + // sql = string.Format(sql, userId); + // _userInfo = DBHelper.Instance.QueryList(sql).SingleOrDefault(); + // Redis.AddObject(userId.ToString(), _userInfo, new TimeSpan(0, 1, 0, 0, 0)); + // } + // return _userInfo ?? new SmUser(); + //} + + ///// + ///// 公司ID + ///// + //public Guid? CompanyId + //{ + // get { return UserInfo.CompanyId ?? Utility.GetCompanyGuidId(); } + //} + + ///// + ///// 集团ID + ///// + //public Guid? GroupId + //{ + // get { return UserInfo.GroupId ?? Utility.GetGroupGuidId(); } + //} + } +}