コード例 #1
0
        public async Task <int> Add(UserRequest userRequest)
        {
            User user = userRequest.ToEntity();;

            if (user.Role != Role.Admin && user.Role != Role.Client)
            {
                throw new InvalidOperationException("New user should have either Admin or Client role.");
            }

            if (userRequest.Id == 0)
            {
                user.Password = HashPassword.GetHashString(user.Password);
                this.context.Users.Add(user);
            }
            else
            {
                User dbUser = await this.context.Users.SingleAsync(u => u.Id == userRequest.Id);

                dbUser.Name     = user.Name;
                dbUser.Email    = user.Email;
                dbUser.Role     = user.Role;
                dbUser.IsActive = user.IsActive;
                this.context.Users.Update(dbUser);
            }

            await this.context.SaveChangesAsync();

            return(user.Id);
        }
コード例 #2
0
        public async Task <UserResponse> Authenticate(string username, string password)
        {
            User user = await this.context.Users.SingleOrDefaultAsync(u => u.Email == username);

            if (user == null)
            {
                return(null);
            }

            if (user.Password == HashPassword.GetHashString(password))
            {
                var response = new UserResponse(user);
                return(response);
            }

            return(null);
        }