示例#1
0
        private IActionResult SetSession(UserViewModel result)
        {
            var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Email, result.Email),
                new Claim(ClaimTypes.Role, result.RoleName)
            }, CookieAuthenticationDefaults.AuthenticationScheme);

            var principal = new ClaimsPrincipal(identity);

            HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

            if (!string.IsNullOrEmpty(result.PasswordExpirayDate) && DateTime.Now.Date <= Convert.ToDateTime(result.PasswordExpirayDate))
            {
                //Handled if image url exist in db but not available physically
                string picpath = hostingEnviroment.WebRootPath + result.ProfilePic;
                if (!System.IO.File.Exists(picpath))
                {
                    string fName = $@"\ProfilePic\" + "Avatar.jpg";
                    result.ProfilePic = fName;
                }
                HttpContext.Session.Set <UserViewModel>(Constants.SessionKeyUserInfo, result);
                authHandler.LogActiveUsers(HttpContext.Session.Id, result);
                authHandler.UserActivity(result.UserId);
                return(GoAhead(result.RoleName, result.UserId));
            }
            else
            {
                return(View("CreateNewPassword"));
            }
        }
示例#2
0
        public IActionResult Login(UserViewModel user)
        {
            try
            {
                var result = authHandler.Login(user.Email.Trim(), user.Password);
                //if (result != null && result.IsApproved == "False")
                //{
                //    throw new NotApprovedByAdminException("Sorry!!! Your account is not activated. Contact your tech deck.");
                //}
                if (null != result)
                {
                    var identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Email, result.Email),
                        new Claim(ClaimTypes.Role, result.RoleName)
                    }, CookieAuthenticationDefaults.AuthenticationScheme);

                    var principal = new ClaimsPrincipal(identity);
                    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    if (!string.IsNullOrEmpty(result.PasswordExpirayDate) && DateTime.Now.Date <= Convert.ToDateTime(result.PasswordExpirayDate))
                    {
                        //Handled if image url exist in db but not available physically
                        string picpath = hostingEnviroment.WebRootPath + result.ProfilePic;
                        if (!System.IO.File.Exists(picpath))
                        {
                            string fName = $@"\ProfilePic\" + "Avatar.jpg";
                            result.ProfilePic = fName;
                        }
                        HttpContext.Session.Set <UserViewModel>(Constants.SessionKeyUserInfo, result);
                        authHandler.UserActivity(result.UserId);
                        return(GoAhead(result.RoleName, result.UserId));
                        //return View("Index");
                    }
                    else
                    {
                        return(View("CreateNewPassword"));
                    }
                }
            }
            catch (InvalidUserCredentialsException ex)
            {
                Logger.Logger.WriteLog(Logger.Logtype.Error, ex.Message, user.UserId, typeof(AuthController), ex);
                ModelState.AddModelError("ErrorMessage", string.Format("{0}", ex.Message));
            }
            catch (UserNotFoundException ex)
            {
                Logger.Logger.WriteLog(Logger.Logtype.Error, ex.Message, user.UserId, typeof(AuthController), ex);
                ModelState.AddModelError("ErrorMessage", string.Format("{0}", ex.Message));
            }
            catch (NotApprovedByAdminException ex)
            {
                Logger.Logger.WriteLog(Logger.Logtype.Error, ex.Message, user.UserId, typeof(AuthController), ex);
                ModelState.AddModelError("ErrorMessage", string.Format("{0}", ex.Message));
            }
            return(View("JobSeekerLogin"));
        }