Example #1
0
 public static UserModel FromUser(User user)
 {
     return new UserModel()
     {
         id = user.Id,
         email = user.Email,
         firstName = user.FirstName,
         lastName = user.LastName
     };
 }
Example #2
0
        public User ToUser(IConfigurationService configService)
        {
            User user = new User()
            {
                Email = email,
                FirstName = firstName,
                LastName = lastName,
                PasswordHash = password == null
                    ? null
                    : HashHelper.GenerateSHA1(configService.PasswordSalt + password)
            };

            if (id != null)
            {
                user.Id = id.Value;
            }

            return user;
        }
 public UserService(TemplateAppContext dbContext)
 {
     _dbContext = dbContext;
     _user = null;
 }
        /// <summary>
        /// Returns the currently logged in user, or <c>null</c> if there is no
        /// user currently logged in.
        /// </summary>
        public async Task<User> GetCurrentUserAsync()
        {
            // get current user (or fetch user)
            User currentUser = _user;
            if (currentUser == null)
            {
                // fetch user based on user id (if any)
                IPrincipal principal = Thread.CurrentPrincipal;
                IIdentity identity = principal == null
                    ? null
                    : principal.Identity;
                if (identity != null
                    && identity.IsAuthenticated)
                {
                    var query =
                        from u in _dbContext.Users
                        where u.Email == identity.Name
                        select u;
                    currentUser = await query.FirstOrDefaultAsync();
                }

                // or use null user
                else
                {
                    currentUser = _nullUser;
                }

                // persist user
                _user = currentUser;
            }

            // return result (or null if it is the null user object)
            return currentUser == _nullUser
                ? null
                : currentUser;
        }
 static UserService()
 {
     // initialize class variables
     _nullUser = new User();
 }