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(); } } public static Microsoft.AspNetCore.Http.HttpContext Context { get { return HttpUseContext.Current; } } /// /// 用户ID /// public int User_Id { get { try { //string aa = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); string userId = Context?.User?.Identity?.Name; return string.IsNullOrEmpty(userId) ? 0 : int.Parse(userId); } catch (Exception) { return 0; //匿名访问 } } } //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(); } //} } }