Ejemplo n.º 1
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.FindByEmailAsync(loginDto.Email);

            if (user == null)
            {
                return(Unauthorized(new ApiResponse(401)));
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized(new ApiResponse(401)));
            }

            return(new UserDto
            {
                Email = user.Email,
                Token = _tokenService.CreateToken(user),
                DisplayName = user.DisplayName
            });
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users
                       .SingleOrDefaultAsync(x => x.Email == loginDto.Email.ToLower());

            if (user == null)
            {
                return(Unauthorized("Invalid email or password"));
            }

            var canSignInawait = _signInManager.CanSignInAsync(user);

            if (!canSignInawait.Result)
            {
                return(Unauthorized("Invalid email or password"));
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <UserDtos> > Login(LoginDto login)
        {
            var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == login.username);

            if (user == null)
            {
                return(Unauthorized("invalid User"));
            }
            using var hmac = new HMACSHA512(user.PasswordSalt);
            var ComputeHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(login.password));

            for (int i = 0; i < ComputeHash.Length; i++)
            {
                if (ComputeHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid Password"));
                }
            }
            return(new UserDtos {
                UserName = user.UserName,
                Token = tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users.SingleOrDefaultAsync(x => x.UserName == loginDto.Username.ToLower());

            if (user == null)
            {
                return(BadRequest("Invalid username"));
            }

            var result = await _singInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            return(new UserDto
            {
                Username = user.UserName,
                FirstName = user.FirstName,
                Token = await _tokenService.CreateToken(user)
            });
        }
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            //We can also use 'FirstOrDefault()' here. d difference is dt 'SingleOrDefault' throws an exception if we have more than one match, 'FirstOrDefault' does not throw an error but returns d first match or null if no match is found
            var user = await _context.Users
                       // Since the 'Photos' property of AppUser belongs to a different entity "Photo.cs" we need to include it as part of details we are returning for a user
                       .Include(appUser => appUser.Photos)
                       .SingleOrDefaultAsync(appUser => appUser.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid Username"));
            }

            //computing and confirming the password harsh using the password salt as key
            using var hmac = new HMACSHA512(user.PasswordSalt);

            //confirming if the passed password is the same as the one used for the harsh
            var computedHarsh = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            //if the password sent does not match the one used for the harsh
            for (int i = 0; i < computedHarsh.Length; i++)
            {
                if (computedHarsh[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid Password"));
                }
            }

            //if they eventually passed all checks, we simply return the UserDto specify values for its fields
            return(new UserDto
            {
                Token = _tokenService.CreateToken(user),
                Username = user.UserName,
                MainImage = user.Photos.FirstOrDefault(p => p.IsMain)?.Url,
                KnownAs = user.KnownAs // we included ds so we can use it in d navbar instead of the username
            });
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto) // This is our login http async method. We pass in the loginDto DTO that we created as the body of the http post request will require an object rather than two strings (username and password)
        {
            var user = await _userManager.Users.                             // We still get access to the Users tables via Identity using the userManager

                       Include(p => p.Photos)

                       .SingleOrDefaultAsync(x => x.UserName == loginDto.Username.ToLower()); // This will return the only element in users that matches our users details. If there is more than one elemt found with the details, it will return an exception. It will return a default value if the username is not found

            if (user == null)
            {
                return(Unauthorized("Invalid Username"));              // We test whether the default value is returned from the SingleOrDefaultAsync (null). If the default value is returned then this means the username was not found in our database. We return the Unauthorised
            }
            // using var hmac = new HMACSHA512(user.PasswordSalt); // This hmac does the reverse of what we did previously. It takes the PasswordSalt and converts it back to the hash. *** As we are now using Identity Framework, we no longer need the Password salt

            // var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password)); // This gets the hash from the password that is passed into the Login method above so that we can compare it below from the one in our database *** As we are now using Identity Framework, we no longer need the Password Hash

            // for (int i = 0; i < computedHash.Length; i++)  // This method loops over every 'byte' in our computedHash and PasswordHash byte array and checks to see if they match. If they do not match (so the password the user enters does not match the password from the database), the the message 'Invalid Password' is returned. *** As we are now using Identity Framework, we no longer need the Password Hash
            // {
            //     if (computedHash[i] != user.PasswordHash[i]) return Unauthorized("Invalid password");
            // }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false); // As we are now using Identity, we sign the user in using this method. We pass in the user object, the password the user has entered. The third parameter is a boolean as this determines whether the user will be locked out if they get the password wrong or not

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            return(new UserDto                                             // We return the userDto so that we are able to access it when the method is called. This is the object that will be returned from our http Register post. We do this as we don;t want to receive the actual user object back as this includes the password etc. We also want to receive the token back as this contains the expiry time
            {
                Username = user.UserName,                                  // we assign the user name to the users user name from the app user we create above
                Token = await _tokenService.CreateToken(user),             // We get our token using the create token method in our token service file
                PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url, // Using the ? optional assignment means that the PhotoUrl is nullable. This means that if there is no IsMain photo, null will be assigned to PhotoUrl.
                KnownAs = user.KnownAs,
                Gender = user.Gender
            });
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users
                       .Include(p => p.Photos)
                       .SingleOrDefaultAsync(u => u.UserName == loginDto.UserName.ToLower());

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }

            // Role managment refactoring
            //using var hmac = new HMACSHA512(user.PasswordSalt);

            // var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            // for (int i = 0; i < computedHash.Length; i++)
            // {
            //     if (computedHash[i] != user.PasswordHash[i]) return Unauthorized("Invalid password");
            // }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                KnowAs = user.KnownAs,
                Gender = user.Gender
            });
        }
Ejemplo n.º 8
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            // var user1 = await _context.Users.SingleOrDefaultAsync(u => u.UserName == loginDto.Username.ToLower());
            // if (user1 == null) return Unauthorized("Invalid Username");

            // using var hmac1 = new HMACSHA512(user1.PasswordSalt);
            // var passwordHash = hmac1.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));
            // if (Enumerable.SequenceEqual(passwordHash, user1.PasswordHash))
            //     return user1;

            // return Unauthorized("Invalid password");

            var user = await _context.Users.Include(p => p.Photos).SingleOrDefaultAsync(user => user.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }

            using var hmac = new HMACSHA512(user.PasswordSalt);

            var ComputedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            for (int i = 0; i < ComputedHash.Length; i++)
            {
                if (ComputedHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid password"));
                }
            }

            return(new UserDto {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user),
                PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
            });
        }
Ejemplo n.º 9
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            /**Retrieve user from database**/
            var user = await _context.Users
                       .SingleOrDefaultAsync(x => x.UserName == loginDto.Username.ToLower());

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }

            /*Retrieve the key of the user passwordSalt*/

            using var hmac = new HMACSHA512(user.PasswordSalt);

            /*Retrieve the passwordHash of the user passing in paratmeter
             *
             * HMAC= Hash Message Authetication Code*/

            var computeHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            /*Apply a loop to compare the passwordHash of user in database and user passing in parameter*/

            for (int i = 0; i < computeHash.Length; i++)
            {
                if (computeHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid password"));
                }
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 10
0
        public async Task <ActionResult <UserDto> > Login(LoginDto login)
        {
            var user = await _userManager.Users.Include(p => p.Photos)
                       .FirstOrDefaultAsync(x => x.Email == login.Email);

            if (user == null)
            {
                return(Unauthorized());
            }
            var result = await _signInManager.CheckPasswordSignInAsync(user, login.Password, false);

            if (result.Succeeded)
            {
                return(new UserDto
                {
                    DisplayName = user.DisplayName,
                    Image = user?.Photos?.FirstOrDefault(x => x.IsMain)?.Url,
                    Token = _tokenService.CreateToken(user),
                    Username = user.UserName
                });
            }

            return(Unauthorized());
        }
Ejemplo n.º 11
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _context.User.SingleOrDefaultAsync(x => x.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("'" + loginDto.Username + "' is an invalid username"));
            }

            using var hmac = new HMACSHA512(user.PasswordSalt);

            var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            if (!Enumerable.SequenceEqual(computedHash, user.PasswordHash))
            {
                return(Unauthorized("Invalid password"));
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 12
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _context.Users.SingleOrDefaultAsync(
                x => x.email == loginDto.email);

            if (user == null)
            {
                return(Unauthorized("Invalid Email"));
            }


            return(new UserDto
            {
                userFirstName = user.userFirstName,

                userLastName = user.userLastName,

                email = user.email,



                Token = _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 13
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users.SingleOrDefaultAsync(credentials => credentials.UserName == loginDto.Username.ToLower());

            if (user == null)
            {
                return(Unauthorized("Username doesn't exist"));
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                KnownAs = user.KnownAs,
                FavoriteColor = user.FavoriteColor
            });
        }
Ejemplo n.º 14
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users
                       .Include(p => p.Photos)
                       .SingleOrDefaultAsync(x => x.UserName == loginDto.Username.ToLower());

            // var result = await _signInManager.SignInAsync(user, true, null);
            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized("Invalid username or password"));
            }


            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                KnownAs = user.KnownAs,
                Gender = user.Gender
            });
        }
Ejemplo n.º 15
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await userManager.Users.FirstOrDefaultAsync(x => x.Email == loginDto.Email);



            if (user == null)
            {
                return(Unauthorized("Invalid email"));
            }
            // await userManager.AddClaimAsync(user, new Claim("IsAdmin","true"));

            var claims = await userManager.GetClaimsAsync(user);

            bool IsAdmin = true;

            if (claims.FirstOrDefault(x => x.Type == "IsAdmin" && x.Value == "true") == null)
            {
                IsAdmin = false;
            }

            var result = await signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (result.Succeeded)
            {
                return(new UserDto
                {
                    Token = tokenService.CreateToken(user),
                    Name = user.Name,
                    Surname = user.Surname,
                    IsAdmin = IsAdmin
                });
            }

            return(Unauthorized("Invalid password"));
        }
Ejemplo n.º 16
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users.SingleOrDefaultAsync(x => x.Email == loginDto.Email.ToLower());

            if (user == null)
            {
                return(Unauthorized("Invalid email"));
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            return(new UserDto
            {
                Email = user.Email,
                Token = await _tokenService.CreateToken(user),
                Id = user.Id,
                Active = user.Active
            });
        }
Ejemplo n.º 17
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _userManager.Users.Include(p => p.Photos)
                       .FirstOrDefaultAsync(x => x.Email == loginDto.Email);

            if (user == null)
            {
                return(Unauthorized("Invalid email"));
            }
            if (!user.EmailConfirmed)
            {
                return(Unauthorized("Email not confirmed"));
            }
            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (result.Succeeded)
            {
                await SetRefreshToken(user);

                return(createUserObject(user));
            }

            return(Unauthorized("Invaild Password"));
        }
        public async Task <ActionResult <UserDto> > LoginUser(LoginDto loginDto)
        {
            //   1)  Check whether user is available
            var userStatus = await _userManager.FindByEmailAsync(loginDto.Email);

            if (userStatus == null)
            {
                return(Unauthorized(new ApiResponse(401)));
            }
            //   2) check whether password is correct
            var passwordStatus = await _signInManager.CheckPasswordSignInAsync(
                userStatus,
                loginDto.Password, false);

            if (!passwordStatus.Succeeded)
            {
                return(Unauthorized(new ApiResponse(401)));
            }
            return(new UserDto {
                DisplayName = userStatus.DisplayName,
                Email = userStatus.Email,
                Token = _tokenService.CreateToken(userStatus)
            });
        }
Ejemplo n.º 19
0
        public async Task <ActionResult <AppUser> > Login(LoginDto loginDto)
        {
            var user = await _context.Users
                       .SingleOrDefaultAsync(x => x.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }

            using var hmac = new HMACSHA512(user.PasswordSalt);

            var ComputeHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.password));

            for (int i = 0; i < ComputeHash.Length; i++)
            {
                if (ComputeHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid password"));
                }
            }

            return(user);
        }
Ejemplo n.º 20
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUserNameAsync(loginDto.UserName.ToLower());

            if (user == null)
            {
                return(Unauthorized("invalid username"));
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }
            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                PhotoUrl = user.Photos?.FirstOrDefault(Photo => Photo.IsMain)?.Url,
                KnownUs = user.KnownUs,
                Gender = user.Gender
            });
        }
Ejemplo n.º 21
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await this.context.Users.SingleOrDefaultAsync(u => u.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }
            using var hmac = new HMACSHA512(user.PasswordSalt);
            var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            for (int i = 0; i < computedHash.Length; i++)
            {
                if (computedHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("Invalid password"));
                }
            }
            return(new UserDto
            {
                Username = user.UserName,
                Token = tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 22
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await userManager.Users.
                       SingleOrDefaultAsync(x => x.UserName == loginDto.Username.ToLower());

            if (user == null)
            {
                return(Unauthorized("Cant find user with the name"));
            }

            var result = await this.signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }


            return(new UserDto
            {
                Username = user.UserName,
                Token = await tokenService.CreateToken(user),
            });
        }
Ejemplo n.º 23
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            User user = await _userManager.FindByEmailAsync(loginDto.Email);

            if (user == null)
            {
                return(BadRequest(new ApiResponse(401)));
            }

            SignInResult result = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);

            if (!result.Succeeded)
            {
                return(BadRequest(new ApiResponse(401)));
            }

            return(new UserDto
            {
                Money = user.Money,
                Email = user.Email,
                Login = user.UserName,
                Token = await _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 24
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _context.User.SingleOrDefaultAsync(x => x.UserName == loginDto.UserName.ToLower());

            if (user == null)
            {
                return(Unauthorized("UserName or Password Do not match! Please try again"));
            }

            using var hmac = new HMACSHA512(user.PasswordSalt);
            var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            for (int i = 0; i < computedHash.Length; i++)
            {
                if (computedHash[i] != user.PasswordHash[i])
                {
                    return(Unauthorized("UserName or Password Do not match! Please try again"));
                }
            }
            return(new UserDto {
                UserName = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            ActionResult <UserDto> res = null;
            string userNameFromLogin   = loginDto.Username.ToLower();
            var    user = await _userManager.Users
                          .Include(p => p.Photos)
                          .SingleOrDefaultAsync(x => x.UserName == userNameFromLogin);

            if (user == null)
            {
                res = Unauthorized("Invalid username");
            }
            else
            {
                var result = await _signInManager
                             .CheckPasswordSignInAsync(user, loginDto.Password, false);

                if (!result.Succeeded)
                {
                    res = Unauthorized();
                }
                else
                {
                    res = new UserDto
                    {
                        Username = user.UserName,
                        Token    = await _tokenService.CreateToken(user),
                        PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                        KnownAs  = user.KnownAs,
                        Gender   = user.Gender,
                    };
                }
            }

            return(res);
        }
Ejemplo n.º 26
0
        public async Task <ActionResult <UserDto> > LoginAsync(LoginDto loginDto)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid username"));
            }

            using var hmac = new HMACSHA512(user.PasswordSalt);

            var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password));

            if (computedHash.Where((t, i) => t != user.PasswordHash[i]).Any())
            {
                return(Unauthorized("Invalid password"));
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 27
0
        public async Task <ActionResult <UserDto> > Login(LoginDto loginDto)
        {
            var user = await _context.Users
                       .Include(p => p.Photos)
                       .SingleOrDefaultAsync(x => x.UserName == loginDto.Username);

            if (user == null)
            {
                return(Unauthorized("Invalid Username"));
            }

            //if (Encoding.UTF8.GetString(user.PasswordHash) != loginDto.Password) return Unauthorized("Invalid Password");
            if ("Pa$$w0rd" != loginDto.Password)
            {
                return(Unauthorized("Invalid Password"));
            }

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user),
                PhotoUrl = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
            });
        }
Ejemplo n.º 28
0
 public async Task <IActionResult> AuthenticateAsync([FromBody] LoginDto loginDto)
 {
     return(await userService.AuthenticateAsync(loginDto.Login, loginDto.Password));
 }
Ejemplo n.º 29
0
 public async Task <IActionResult> Login(LoginDto loginDto)
 {
     return(HandleResult(await Mediator.Send(new Login.Query(loginDto))));
 }
 public async Task <ActionResult <JwtAuthDto> > LoginAsync([FromBody] LoginDto loginDto)
 {
     return(await _authHelper.LoginMemberAsync(loginDto));
 }