Esempio n. 1
0
        public async Task <UserManagerResponse <UserForLoginDto> > ForgetPasswordAsync(string email, IMailRepository mailRepository)
        {
            if (email == null)
            {
                throw new NullReferenceException("email cannot be null.");
            }

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                return(new UserManagerResponse <UserForLoginDto>
                {
                    Message = "No user associated with this email",
                    IsSuccess = false
                });
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            var encodedToken = Encoding.UTF8.GetBytes(token);

            var validToken = WebEncoders.Base64UrlEncode(encodedToken);

            string url = $"{_config["AppUrl"]}/ResetPassword?email={email}&token={validToken}";

            await mailRepository.SendEmailAsync(user.Email, "Reset password", $"<h2>Follow the instruction to reset your password</h2>" +
                                                $"<p>To reset your password <a href='{url}'>click here</a></p>");

            return(new UserManagerResponse <UserForLoginDto>
            {
                Message = "Reset password URL has been sent to your email Suceessfully.",
                IsSuccess = true
            });
        }
Esempio n. 2
0
        public async Task <UserManagerResponse <UserForRegisterDto> > RegisterUserAsync(User userForRegisterDto, IMailRepository mailRepository, string password)
        {
            CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
            userForRegisterDto.PasswordHash = passwordHash;
            userForRegisterDto.PasswordSalt = passwordSalt;

            // Generate email token and send confirmation email
            GenerateEmailConfirmationToken(out byte[] emailTokenHash, out byte[] emailTokenSalt);

            var userToCreate = new User
            {
                UserName       = userForRegisterDto.UserName,
                Email          = userForRegisterDto.Email,
                PasswordSalt   = userForRegisterDto.PasswordSalt,
                PasswordHash   = userForRegisterDto.PasswordHash,
                EmailTokenSalt = emailTokenSalt,
                Role           = "Admin",
                UserRole       = userForRegisterDto.Role
            };

            //Register new User
            await _db.Users.AddAsync(userToCreate);

            int result = await _db.SaveChangesAsync();

            if (result > 0)
            {
                var user = await _db.Users.SingleOrDefaultAsync(u => u.Email == userForRegisterDto.Email);

                // Generate email token and send confirmation email

                //GenerateEmailConfirmationToken(out byte[] emailTokenHash, out byte[] emailTokenSalt);

                var confirmEmailToken = Encoding.UTF8.GetString(emailTokenHash);

                var encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailToken);

                var validEmailToken = WebEncoders.Base64UrlEncode(encodedEmailToken);

                string url = $"{_config["AppUrl"]}/api/auth/confirmemail?userId={user.Id}&token={validEmailToken}";

                await mailRepository.SendEmailAsync(user.Email, "Please Confirm your email", $"<h2>Welcome to Auth Demo</h2>" +
                                                    $"<p>Please confirm your email by <a href='{url}'>clicking here</a></p>");
            }

            var userToDto = new UserForRegisterDto
            {
                UserName = userToCreate.UserName,
                Email    = userToCreate.Email
            };

            return(new UserManagerResponse <UserForRegisterDto>
            {
                Message = "User Authentication Successful",
                IsSuccess = true,
                SingleData = userToDto
            });
        }
Esempio n. 3
0
        public async Task <UserManagerResponse <UserForLoginDto> > LoginUserAsync(UserForLoginDto userForLoginDto, IMailRepository mailRepository)
        {
            if (userForLoginDto == null)
            {
                throw new NullReferenceException("Login model not found.");
            }

            var user = await _userManager.FindByEmailAsync(userForLoginDto.Email);

            if (user == null)
            {
                return(new UserManagerResponse <UserForLoginDto>
                {
                    Message = "User not found.",
                    IsSuccess = false
                });
            }
            var result = await _userManager.CheckPasswordAsync(user, userForLoginDto.Password);

            //.CheckPasswordSignInAsync(user, userForLoginDto.Password, false);
            if (!result)
            {
                return(new UserManagerResponse <UserForLoginDto>
                {
                    Message = "Invalid Password.",
                    IsSuccess = false
                });
            }

            await mailRepository.SendEmailAsync(userForLoginDto.Email, "New Login", "<h1>New Login Notice</h1><p>Account was logged into at " + DateTime.Now + "</p>");

            var generatedToken = GenerateJwToken(user);

            return(new UserManagerResponse <UserForLoginDto>
            {
                Message = "Login Successful",
                IsSuccess = true,
                ExpiredDate = generatedToken.TokenExpiredDate,
                Token = generatedToken.Token
            });
        }
Esempio n. 4
0
        public async Task <UserManagerResponse <UserForRegisterDto> > RegisterUserAsync(UserForRegisterDto userForRegisterDto, IMailRepository mailRepository)
        {
            if (userForRegisterDto == null)
            {
                throw new NullReferenceException("Register model not found.");
            }

            if (userForRegisterDto.Password != userForRegisterDto.ConfirmPassword)
            {
                return new UserManagerResponse <UserForRegisterDto>
                       {
                           Message   = "Password and Confirm Password does not match",
                           IsSuccess = false
                       }
            }
            ;

            var userToCreate = new ApplicationUser
            {
                UserName = userForRegisterDto.Username,
                Email    = userForRegisterDto.Email
            };

            //instead of using the identity provided createAsync function, you can use your own logic to register the user in the db
            var result = await _userManager.CreateAsync(userToCreate, userForRegisterDto.Password);

            if (result.Succeeded)
            {
                // TODO: You can add roles to user here



                var user = await _userManager.FindByEmailAsync(userForRegisterDto.Email);

                //TODO: Send confirmation email
                var confirmEmailToken = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailToken);

                var validEmailToken = WebEncoders.Base64UrlEncode(encodedEmailToken);

                string url = $"{_config["AppUrl"]}/api/auth/confirmemail?userId={user.Id}&token={validEmailToken}";

                await mailRepository.SendEmailAsync(user.Email, "Please Confirm your email", $"<h2>Welcome to Auth Demo</h2>" +
                                                    $"<p>Please confirm your email by <a href='{url}'>clicking here</a></p>");

                var userToDto = new UserForRegisterDto
                {
                    Username = user.UserName,
                    Email    = user.Email
                };
                return(new UserManagerResponse <UserForRegisterDto>
                {
                    SingleData = userToDto,
                    Message = "User Created Suceessfully.",
                    IsSuccess = true
                });
            }
            return(new UserManagerResponse <UserForRegisterDto>
            {
                Message = "User not Created.",
                IsSuccess = false,
                Errors = result.Errors.Select(x => x.Description)
            });
        }