예제 #1
0
        public async Task <IActionResult> LogIn(string email = "", string password = "")
        {
            try
            {
                CheckNotNull(email, password);
                string      hashedPassword = _passwordHasher.Hash(password);
                UserAccount user           = UserAccountBLL.Authenticate(email, hashedPassword, UserAccountTypes.Employee);

                if (user == null)
                {
                    throw new Exception("Email or password is not valid.");
                }

                var claims = new List <Claim>
                {
                    new Claim("UserID", user.UserID),
                    new Claim("FullName", user.Fullname),
                    new Claim(ClaimTypes.Role, user.Groupname),
                    new Claim("LoginTime", Convert.ToString(DateTime.Now)),
                    new Claim("ClientIP", _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString()),
                    new Claim("Photo", user.Photo),
                    new Claim("Title", user.Title),
                };
                await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, "UserInfo")));

                return(RedirectToAction("Index", "Dashboard"));
            }
            catch (MissingFieldException)
            {
                ViewData["email"]    = email ?? "";
                ViewData["password"] = password ?? "";

                return(View());
            }
            catch (System.Exception ex)
            {
                ViewData["email"]    = email ?? "";
                ViewData["password"] = password ?? "";

                ModelState.AddModelError("LoginError", ex.Message);
                return(View());
            }
        }