示例#1
0
        public void IsEqual_WhenEqualPassword()
        {
            var password       = "******";
            var hashedPassword = AuthOptions.ComputeHash(password);

            var password1 = "zxcvQWER45";
            var password2 = "zxcvQWER45";

            Assert.That(AuthOptions.ComputeHash(password1), Is.EqualTo(AuthOptions.ComputeHash(password2)));
            Assert.That(AuthOptions.ComputeHash("Zxqw1290P"), Is.EqualTo(hashedPassword));
        }
示例#2
0
        private User GetUser(string login, string password)
        {
            var potentialUser = _context.Users
                                .FirstOrDefault(user => user.Email == login);

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

            if (potentialUser.Password == AuthOptions.ComputeHash(password))
            {
                return(potentialUser);
            }

            return(null);
        }
示例#3
0
        public async Task <IActionResult> PostUser([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var equalUser = _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email);

            if (equalUser.Result != null)
            {
                return(Conflict("Пользователь уже существует"));
            }

            user.Password = AuthOptions.ComputeHash(user.Password);
            var jwt = AuthOptions.GetJWT(user);

            var rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

            rootPath = Directory.GetParent(rootPath).FullName;
            rootPath = Directory.GetParent(rootPath).FullName;
            rootPath = Directory.GetParent(rootPath).FullName;

            using (var fstream = new FileStream(rootPath + @"\etc\anon.png", FileMode.OpenOrCreate))
            {
                var byteArray = new byte[fstream.Length];
                fstream.Read(byteArray, 0, byteArray.Length);
                user.Avatar = byteArray;
            }

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            var response = new
            {
                user.Id,
                email    = user.Email,
                name     = user.Name,
                lastName = user.LastName,
                token    = jwt
            };

            return(Ok(response));
        }
示例#4
0
        public void IsNotEqual_WhenDifferentPasswords()
        {
            var password1 = "Zxqw1290P";
            var password2 = "Zxqw1209P";

            var hash1 = AuthOptions.ComputeHash(password1);
            var hash2 = AuthOptions.ComputeHash(password2);

            var password3 = "Zxwq1290P";
            var password4 = "Zxqw1290P";

            var hash3 = AuthOptions.ComputeHash(password3);
            var hash4 = AuthOptions.ComputeHash(password4);

            var password5 = "Zxqw1290P";
            var password6 = "Zxew1290P";

            var hash5 = AuthOptions.ComputeHash(password5);
            var hash6 = AuthOptions.ComputeHash(password6);

            Assert.That(hash1, Is.Not.EqualTo(hash2));
            Assert.That(hash3, Is.Not.EqualTo(hash4));
            Assert.That(hash5, Is.Not.EqualTo(hash6));
        }