Facebook
From Mahedi Hasan Niloy, 1 Month ago, written in ASP.
Embed
Download Paste or View Raw
Hits: 118
  1. [AllowAnonymous]
  2.         [HttpPost]
  3.  
  4.         public ActionResult Login(FormCollection formCollection)
  5.         {
  6.  
  7.             if (IsSameBrowserLogIn())
  8.             {
  9.                 AddToastMessage("", $"Already logged in with {Sessions.Name.UserName}, to login with other user please logout from current user.", ToastType.Error);
  10.                 return View();
  11.             }
  12.  
  13.             var userName = formCollection["UserName"];
  14.             var password = formCollection["Password"];
  15.             var keepLogin = formCollection["keepLogin"];
  16.             bool keepLoginSession;
  17.  
  18.             keepLoginSession = keepLogin == "on";
  19.  
  20.             if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
  21.             {
  22.                 AddToastMessage("", "Please enter valid username and password", ToastType.Error);
  23.                 return View();
  24.             }
  25.  
  26.             var appUserInfo = _accountService.GetAppUserInfoByUserID(userName);
  27.  
  28.             bool passwordMatch = BCrypt.Net.BCrypt.Verify(password, appUserInfo.password);
  29.  
  30.             if (appUserInfo != null && passwordMatch)
  31.             {
  32.  
  33.                 // Jwt Authentication code
  34.                 if (appUserInfo != null)
  35.                 {
  36.                     string encryptedPwd = password;
  37.  
  38.                     var userPassword = appUserInfo.password;
  39.                     var username = appUserInfo.user_emp_code;
  40.                     if (encryptedPwd.Equals(userPassword) && username.Equals(userName))
  41.                     {
  42.                         var role = appUserInfo.user_type_name;
  43.                         var jwtToken = Authentication.GenerateJWTAuthetication(userName, role);
  44.                         var validUserName = Authentication.ValidateToken(jwtToken);
  45.  
  46.                         if (string.IsNullOrEmpty(validUserName))
  47.                         {
  48.                             AddToastMessage("", "Unauthorized login attempt ", ToastType.Error);
  49.                             return View();
  50.                         }
  51.                     }
  52.                 }
  53.  
  54.                 if (appUserInfo != null && appUserInfo.is_active == true)
  55.                 {
  56.                     InitializeSession(appUserInfo, keepLoginSession);
  57.                     ResetFailedAttempts(appUserInfo);
  58.  
  59.                     AddToastMessage("", "Login Successfully", ToastType.Success);
  60.                     return RedirectToAction("Index", "DashBoard");
  61.                 }
  62.                 else if (appUserInfo != null && appUserInfo.is_active == false)
  63.                 {
  64.                     AddToastMessage("", "This user is currently inactive", ToastType.Warning);
  65.                     return View();
  66.                 }
  67.                 else
  68.                 {
  69.                     var userInfo = _accountService.GetApplicationUserByEmpCode(userName);
  70.  
  71.                     if (userInfo != null)
  72.                     {
  73.                         IncrementFailedAttempts(userInfo);
  74.  
  75.                         if (userInfo.FailedAttempt >= 5)
  76.                         {
  77.                             LockUserAccount(userInfo);
  78.                             AddToastMessage("",
  79.                                 "Your account has been locked due to too many failed login attempts. Please contact an administrator to reactivate your account.",
  80.                                 ToastType.Error);
  81.                             return View();
  82.                         }
  83.                     }
  84.  
  85.  
  86.                     AddToastMessage("", "Invalid User ID or Password", ToastType.Error);
  87.                     return View();
  88.                 }
  89.             }
  90.  
  91.             else
  92.             {
  93.                 AddToastMessage("", "Invalid User ID or Password", ToastType.Error);
  94.                 return View();
  95.             }
  96.            
  97.         }
  98.  
  99.         private void InitializeSession(ApplicationUser appUserInfo, bool keepLoginSession)
  100.         {
  101.             var httpSession = Session;
  102.  
  103.             if (Sessions.Name == null)
  104.             {
  105.                 Sessions.Name = new SessionInfo();
  106.             }
  107.  
  108.             Sessions.Name.UserId = appUserInfo.user_emp_code;
  109.             Sessions.Name.UserName = appUserInfo.user_emp_name;
  110.             Sessions.Name.UserTypeId = appUserInfo.user_type_id;
  111.             Sessions.Name.SessionStart = DateTime.Now;
  112.             Sessions.Name.KeepLogin = keepLoginSession;
  113.             Sessions.Name.SessionKey = httpSession.SessionID;
  114.         }
  115.  
  116.         private void ResetFailedAttempts(ApplicationUser appUserInfo)
  117.         {
  118.             appUserInfo.FailedAttempt = 0;
  119.             _accountService.UpdateAppUser(appUserInfo);
  120.             _accountService.SaveAppUser();
  121.         }
  122.  
  123.         private void IncrementFailedAttempts(ApplicationUser userInfo)
  124.         {
  125.             userInfo.FailedAttempt++;
  126.             _accountService.UpdateAppUser(userInfo);
  127.         }
  128.  
  129.         private void LockUserAccount(ApplicationUser userInfo)
  130.         {
  131.             userInfo.is_active = false;
  132.             _accountService.UpdateAppUser(userInfo);
  133.             _accountService.SaveAppUser();
  134.         }
  135.  
  136.  
  137.         private bool IsSameBrowserLogIn()
  138.         {
  139.             var data = Session;
  140.             if (Session != null && Sessions.Name != null && data.SessionID == Sessions.Name.SessionKey)
  141.             {
  142.                 return true;
  143.             }
  144.             else
  145.             {
  146.                 return false;
  147.             }
  148.         }