Example #1
0
        public IActionResult Register(Users users)
        {
            System.Console.WriteLine("register path");
            if (ModelState.IsValid)
            {
                System.Console.WriteLine("passed is valid");
                if (_dbConnector.Users.Any(u => u.Email == users.Email))
                {
                    ModelState.AddModelError("email", "email is already in use");
                    System.Console.WriteLine("entered not valid email");
                    return(View("Index"));
                }
                PasswordHasher <Users> Hasher = new PasswordHasher <Users>();
                users.Password = Hasher.HashPassword(users, users.Password);

                _dbConnector.Add(users);
                _dbConnector.SaveChanges();
                HttpContext.Session.SetInt32("UserId", users.UserId);
                HttpContext.Session.SetString("Name", users.Name);
                HttpContext.Session.SetString("Alias", users.Alias);
                System.Console.WriteLine("SUCCESS!!");
                return(RedirectToAction("Success"));
            }
            else
            {
                System.Console.WriteLine("failed registration");
                return(View("Index"));
            }
        }
        public async Task <ActionResult <UserDTO> > register(RegisterDTO reg)
        {
            if (await isExist(reg.name))
            {
                return(BadRequest("User Name Is Exist, Please try another name"));
            }

            using var hmack = new HMACSHA512();
            var user = new user
            {
                Username     = reg.name.ToLower(),
                passwordHash = hmack.ComputeHash(Encoding.UTF8.GetBytes(reg.password)),
                passwordSalt = hmack.Key
            };

            db.Add(user);
            await db.SaveChangesAsync();

            return(new UserDTO {
                name = user.Username, token = await jwt.createToken(user)
            });
        }