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(); })); }
public static UserQuickViewModel QuickView(this IPrincipalUser principal) { if (principal.Id == 0) { return(new UserQuickViewModel()); } return((UserQuickViewModel)principal.Cache().GetOrAdd(nameof(UserQuickViewModel), key => { using var scope = principal.ServiceProvider.CreateScope(); var db = scope.ServiceProvider.GetRequiredService <IUsersDbContext>(); var quickView = db.Users .AsNoTracking() .Where(x => x.Id == principal.Id) .Select(x => new UserQuickViewModel { 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 = Todo.NoNeed }) .SingleOrDefault(); if (quickView == null) { principal.Auth().SignOut(); } return quickView ?? new UserQuickViewModel(); })); }