Exemplo n.º 1
0
 public static UserModel FromUser(User user)
 {
     return new UserModel()
     {
         id = user.Id,
         email = user.Email,
         firstName = user.FirstName,
         lastName = user.LastName
     };
 }
Exemplo n.º 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;
        }
Exemplo n.º 3
0
 public UserService(TemplateAppContext dbContext)
 {
     _dbContext = dbContext;
     _user = null;
 }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
 static UserService()
 {
     // initialize class variables
     _nullUser = new User();
 }