Exemple #1
0
        public IActionResult Login(string username, string password)
        {
            if (HttpContext.Session.GetString("username").IsNullOrEmpty()) //used to transfer viewbag data back to view
            {
                StartSession();
            }
            else
            {
                TempData["empCheck"] = JsonConvert.SerializeObject(empCheck);
            }

            if (username != null || !HttpContext.Session.GetString("username").IsNullOrEmpty())
            {
                if (!HttpContext.Session.GetString("username").IsNullOrEmpty())
                {
                    empCheck = JsonConvert.DeserializeObject <Employee>(HttpContext.Session.GetString("employee")) as Employee;
                    return(RedirectToAction("Dashboard"));
                }
                empCheck = empService.GetEmployee(username, password);
                if (empCheck != null)
                {
                    HttpContext.Session.SetString("firstName", empCheck.Firstname);
                    HttpContext.Session.SetString("lastName", empCheck.Lastname);
                    HttpContext.Session.SetString("username", empCheck.Username);
                    HttpContext.Session.SetInt32("id", empCheck.Id);
                    HttpContext.Session.SetString("empType", empCheck.EmployeeType.EmployeeTypeName);
                    HttpContext.Session.SetString("employee", JsonConvert.SerializeObject(empCheck));
                    bool isTempDeptHead = delService.CheckEmpIsTempDeptHeadToday(empCheck);
                    if (isTempDeptHead == true)
                    {
                        HttpContext.Session.SetString("tempDeptHead", empCheck.TempDeptHeadType.EmployeeTypeName);
                    }

                    //Add AuthCookie

                    var userClaims = new List <Claim>()
                    {
                        new Claim(ClaimTypes.Name, empCheck.Username),
                        new Claim(ClaimTypes.Role, empCheck.EmployeeType.EmployeeTypeName),
                    };

                    var grandmaIdentity = new ClaimsIdentity(userClaims, CookieAuthenticationDefaults.AuthenticationScheme);

                    var userPrincipal = new ClaimsPrincipal(new[] { grandmaIdentity });

                    HttpContext.SignInAsync(userPrincipal);

                    return(RedirectToAction("Dashboard"));
                }
            }

            return(View("Login"));
        }