public ActionResult Login(TaiKhoanModels model, string returnUrl, string captcha) { _logger.Start("Login"); bool bCaptcha = false; ViewBag.isNhapCaptcha = false; LoginServices service = new LoginServices(); try { //neu so lan user dang nhap vuot qua gioi han se bi lock if (Session[strSessionLoginFail] == null) { Session[strSessionLoginFail] = 0; } int loginFail = (int)Session[strSessionLoginFail]; if (!string.IsNullOrEmpty(captcha) && !string.IsNullOrWhiteSpace(captcha)) { if (captcha == Session[strSessionCaptcha].ToString()) { bCaptcha = true; } } if (loginFail < RICONS.Core.Constants.CST_Common.CST_Lock || bCaptcha) { #region kiem tra user login tren he thong AD admin bool validAD = false; string matkhaudangnnhap = model.matkhau; string tendangnhap = model.tendangnhap.Trim().Split('@')[0].ToLower(); var passWordDecrypt = EncDec.DecryptStringAES(model.matkhau); try { using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "newtecons.vn", tendangnhap, passWordDecrypt)) { validAD = context.ValidateCredentials(tendangnhap, passWordDecrypt); UserPrincipal user_ad = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, tendangnhap); if (validAD) { #region var user = service.GetLoginData(new M_TaiKhoan() { tendangnhap = tendangnhap, xoa = CST_Common.CST_NOT_DELETE, kichhoat = CST_Common.CST_ACTIVE }); TaiKhoanServices serTaiKhoan = new TaiKhoanServices(); TaiKhoanModels modeltk = new TaiKhoanModels(); // Lay du lieu employee //var dlemployee = serTaiKhoan.GetDataEmployee(tendangnhap); if (user == null) { #region //if (dlemployee != null) //{ // modeltk.manhansu = dlemployee.manhansu; // modeltk.hoten = dlemployee.hoten; // modeltk.machucdanh = dlemployee.machucdanh; // modeltk.tenchucdanh = dlemployee.tenchucdanh; // modeltk.maphongban = dlemployee.maphongban; // if (dlemployee.phongban_congtruong.ToString().ToLower() == "false") // modeltk.phongban_congtruong = "0"; // else modeltk.phongban_congtruong = "1"; //} modeltk.tendangnhap = tendangnhap; model.madonvi = 1; modeltk.thudientu = user_ad.EmailAddress; modeltk.chucdanhkpi = "1"; bool kq = serTaiKhoan.AddUser(modeltk, 0); user = service.GetLoginData(new M_TaiKhoan() { tendangnhap = tendangnhap, xoa = CST_Common.CST_NOT_DELETE, kichhoat = CST_Common.CST_ACTIVE }); #endregion } //else if (dlemployee != null) //{ // //Cập nhật manhansu,machucdanh,maphongban // user.manhansu = dlemployee.manhansu; // user.maphongban = dlemployee.maphongban; // user.machucdanh = dlemployee.machucdanh; // user.tenchucdanh = dlemployee.tenchucdanh; // user.hoten = dlemployee.hoten; // user.sodienthoai = dlemployee.sodienthoai; // user.thudientu = user_ad.EmailAddress; // user.ngaysinh = dlemployee.ngaysinh; // user.chucdanhkpi = user.chucdanhkpi; // if(dlemployee.phongban_congtruong.ToString().ToLower()=="false") // user.phongban_congtruong = "0"; // else user.phongban_congtruong = "1"; // if (user.chucdanhkpi.Trim() == "" || user.chucdanhkpi == null) // user.chucdanhkpi = "1"; // bool kqcapnhat = serTaiKhoan.Updatemanhansu(user.mataikhoan, user.manhansu, user.machucdanh.ToString(), user.maphongban, user.hoten, user.chucdanhkpi, user.phongban_congtruong); //} var lstPhongBanDonVis = serTaiKhoan.SelectPhongBanDonVi(new M_TaiKhoan() { mataikhoan = user.mataikhoan }); user.phongBanDonVis = lstPhongBanDonVis; AddSession(user); UpdateLoginState(tendangnhap, false, loginFail); //chuyen trang _logger.End("Login"); return(RedirectToAction("Index", "WeedMeeting")); //return RedirectToLocal(returnUrl); #endregion } else if (!validAD) { #region var user = service.GetLoginData(new M_TaiKhoan() { tendangnhap = model.tendangnhap, matkhau = EncDec.EncodePassword(passWordDecrypt), xoa = CST_Common.CST_NOT_DELETE, kichhoat = CST_Common.CST_ACTIVE }); if (user != null) { TaiKhoanServices serTaiKhoan = new TaiKhoanServices(); var lstPhongBanDonVis = serTaiKhoan.SelectPhongBanDonVi(new M_TaiKhoan() { mataikhoan = user.mataikhoan }); user.phongBanDonVis = lstPhongBanDonVis; AddSession(user); UpdateLoginState(model.tendangnhap, false, loginFail); _logger.End("Login"); return(RedirectToAction("Index", "WeedMeeting")); } else { try { UpdateLoginState(model.tendangnhap, true, loginFail); } catch (Exception ex) { _logger.Error(ex); } } #endregion } } } catch (Exception ex) { validAD = false; _logger.Info(ex); } #endregion #region kiem tra user login local if (!validAD) { var user = service.GetLoginData(new M_TaiKhoan() { tendangnhap = model.tendangnhap, matkhau = EncDec.EncodePassword(passWordDecrypt), xoa = CST_Common.CST_NOT_DELETE, kichhoat = CST_Common.CST_ACTIVE }); if (user != null) { TaiKhoanServices serTaiKhoan = new TaiKhoanServices(); var lstPhongBanDonVis = serTaiKhoan.SelectPhongBanDonVi(new M_TaiKhoan() { mataikhoan = user.mataikhoan }); user.phongBanDonVis = lstPhongBanDonVis; AddSession(user); UpdateLoginState(model.tendangnhap, false, loginFail); _logger.End("Login"); return(RedirectToAction("Index", "WeedMeeting")); } else { try { UpdateLoginState(model.tendangnhap, true, loginFail); } catch (Exception ex) { _logger.Error(ex); } } } #endregion } else { ViewBag.isNhapCaptcha = true; UpdateLoginState(model.tendangnhap, true, loginFail); } } catch (Exception ex) { _logger.Error(ex); _logger.End("CheckLogin"); } // If we got this far, something failed, redisplay form return(View(model)); }