public static UserInfoViewModel User(this IPrincipalUser principal)
        {
            if (principal.Id == 0 || !(principal.SessionData() is SessionDataContainer sessionData))
            {
                return(new UserInfoViewModel());
            }

            return((UserInfoViewModel)sessionData.GetOrAdd(nameof(UserInfoViewModel), key => {
                using var scope = principal.ServiceProvider.CreateScope();
                var db = scope.ServiceProvider.GetRequiredService <IUsersDbContext>();

                var userInfo = db.Users
                               .AsNoTracking()
                               .Where(x => x.Id == principal.Id)
                               .Select(x => new UserInfoViewModel {
                    PhotoUrl = x.PhotoUrl ?? (x.WeChat == null ? null : x.WeChat.HeadImageUrl),
                    PhoneNumber = x.Phone == null ? null : x.Phone.Number,
                    EmailAddress = x.Email == null ? null : x.Email.EmailAddress,
                    RegisteredTime = x.RegisteredTime,
                    AwaitChangePassword = ActionAwait.NoNeed,
                    IsTwoFactorValidated = false
                })
                               .SingleOrDefault();

                if (userInfo == null)
                {
                    principal.Auth().SignOut();
                }
                return userInfo ?? new UserInfoViewModel();
            }));
        }
Example #2
0
 internal DateTime GetTimer()
 {
     return(_me.Id != 0 && _me.SessionData() is SessionDataContainer sessionData
                         ? (DateTime)sessionData.GetOrAdd(nameof(AntiViolenceBlocker), key => DateTime.Now.AddDays(-1))
                         : DateTime.MinValue);
 }