Exemplo n.º 1
0
        private async Task <Guid> JoinUserAsync(JoinModel model, string type)
        {
            var localHash    = _cryptoProvider.GetRandomSaltString();
            var passwordHash = _cryptoProvider.GetPasswordHash(model.PasswordString, localHash);

            User user = new User
            {
                UserId       = Guid.NewGuid(),
                Email        = model.Email,
                Phone        = model.Phone,
                LocalHash    = localHash,
                PasswordHash = passwordHash,
                LastName     = model.LastName,
                FirstName    = model.FirstName,
                Role         = _db.Roles.FirstOrDefault(x => x.Name == type.ToUpper())
            };

            var tt = user;

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

            return(user.UserId);
        }
Exemplo n.º 2
0
        private async Task <AuthStatusResult> AuthenticateUser(AuthModel model)
        {
            var user = await _db.Users.SingleOrDefaultAsync(x => x.Phone == model.UserString || x.Email == model.UserString);

            //If user write email/phone that doesnt exist in db
            if (user == null)
            {
                _authResult.IncorrectData = true;
                _authResult.isSuccessful  = false;

                return(_authResult);
            }

            var enteredPassword = _cryProvider.GetPasswordHash(model.Password, user.LocalHash);

            //If password is incorrect
            if (!user.PasswordHash.SequenceEqual(enteredPassword))
            {
                _authResult.IncorrectPassword = true;
                _authResult.isSuccessful      = false;

                return(_authResult);
            }

            //all credentials are true
            List <Claim> userClaims = await VerifyUserAsync(user);

            if (userClaims == null)
            {
                _authResult.IncorrectData = true;
                _authResult.isSuccessful  = false;

                return(_authResult);
            }

            var u_id            = new ClaimsIdentity(userClaims, "ApplicationCookie");
            var claimsPrincipal = new ClaimsPrincipal(u_id);

            await AuthenticationHttpContextExtensions.SignInAsync(_contextAcessor.HttpContext, claimsPrincipal);

            _logger.LogInformation($"User email: {user.Email} has been authenticated");

            return(_authResult);
        }