Пример #1
0
        public async Task <IActionResult> SignIn(string email, string password)
        {
            var f_password = new GravatarImage().hashBuilder(password);

            User user = await _context.user.FirstOrDefaultAsync(u => u.email.Equals(email) && u.pw_hash.Equals(f_password));

            if (user != null)
            {
                var claims = new List <Claim>
                {
                    new Claim("UserEmail", user.email),
                    new Claim("Username", user.username),
                    new Claim("UserID", user.user_id.ToString())
                };
                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));

                //_logger.LogInformation("API user {userID} successfully signed in.", user.user_id.ToString());
                return(NoContent());
            }
            else
            {
                //_logger.LogWarning("API user failed to successfully sign in.");
                return(BadRequest("Wrong email or password"));
            }
        }
        public async Task <IActionResult> SignIn(string email, string pw_hash)
        {
            if (!userExistDB())
            {
                await Sign_Out();
            }

            if (ModelState.IsValid && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(pw_hash))
            {
                GravatarImage newHash    = new GravatarImage();
                var           f_password = newHash.hashBuilder(pw_hash);
                var           data       = _context.user.Where(u => u.email.Equals(email) && u.pw_hash.Equals(f_password)).ToList();
                if (data.Count() > 0)
                {
                    User user = await _context.user.FirstOrDefaultAsync(u => u.email.Equals(email) && u.pw_hash.Equals(f_password));

                    if (user != null)
                    {
                        var claims = new List <Claim>
                        {
                            new Claim("UserEmail", user.email),
                            new Claim("Username", user.username),
                            new Claim("UserID", user.user_id.ToString())
                        };
                        var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));

                        _logger.LogInformation("{userID}, just logged into the website!", user.user_id.ToString());
                        return(RedirectToAction("Timeline"));
                    }
                }
                else
                {
                    ViewBag.error          = "Login failed";
                    ViewData["testOutput"] = "Login failed";

                    _logger.LogWarning("A user has failed to login!");
                    return(RedirectToAction("SignIn"));
                }
            }
            ViewBag.error = "Login failed";
            return(View());
        }
 public IActionResult SignUp(User _user)
 {
     if (ModelState.IsValid)
     {
         var check = _context.user.FirstOrDefault(u => u.email == _user.email);
         if (check == null)
         {
             GravatarImage newHash = new GravatarImage();
             _user.pw_hash = newHash.hashBuilder(_user.pw_hash);
             _context.user.Add(_user);
             _context.SaveChanges();
             return(RedirectToAction("SignIn"));
         }
         else
         {
             ViewBag.error = "Email already exist";
             return(View());
         }
     }
     return(View());
 }
Пример #4
0
 public IActionResult SignUp(User _user)
 {
     if (ModelState.IsValid)
     {
         var check = _context.user.FirstOrDefault(u => u.email == _user.email);
         if (check == null)
         {
             GravatarImage newHash = new GravatarImage();
             _user.pw_hash = newHash.hashBuilder(_user.pw_hash);
             _context.user.Add(_user);
             _context.SaveChanges();
             _logger.LogInformation("New user just signed up to the website!");
             return(RedirectToAction("SignIn"));
         }
         else
         {
             _logger.LogWarning("A user has failed to signup! E-mail already exists in the system.");
             ViewBag.error = "Email already exist";
             return(View());
         }
     }
     return(View());
 }