public ActionResult Login(LoginJson model, string returnUrl, string submitCode) { string culture = "en-US"; if (Session["Culture"] != null) { culture = Session["Culture"].ToString(); } Session.RemoveAll(); Session["Culture"] = culture; if (submitCode == "Retrimite codul") { model.Code = null; //string rnd = "1234"; // pt. test Utilizator u = (Utilizator)TempData["tempLogin"]; Random generator = new Random(); string rnd = generator.Next(0, 1000000).ToString("D6"); SendVerificationCode(EmailProfiles.AwsCereriSES, u.EMAIL, rnd); TempData.Clear(); TempData["tempLogin"] = u; TempData["verificationCode"] = rnd; TempData["verificationCodeIssueTime"] = DateTime.Now; return(View("Login", new LoginJson())); } if ((TempData["tempLogin"] == null || String.IsNullOrWhiteSpace(TempData["tempLogin"].ToString())) && String.IsNullOrWhiteSpace(model.Username)) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.EMPTY_USERNAME); } if ((TempData["tempLogin"] == null || String.IsNullOrWhiteSpace(TempData["tempLogin"].ToString())) && String.IsNullOrWhiteSpace(model.Password)) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.EMPTY_PASSWORD); } if (!ModelState.IsValid) { return(View(model)); } if (Session["conStr"] == null) // pt. relogin dupa expirare sesiune sau inactivitate { string conStr = Server.MapPath("~").ToLower().IndexOf("test") > 0 ? CommonFunctions.StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["MySQLConnectionString_test"].ConnectionString, CommonFunctions.StringCipher.RetrieveKey()) : CommonFunctions.StringCipher.Decrypt(System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString, CommonFunctions.StringCipher.RetrieveKey()); // separam socisa de socisa_test Session["conStr"] = conStr; } UtilizatoriRepository ur = new UtilizatoriRepository(null, Session["conStr"].ToString()); try { FormsAuthentication.SignOut(); } catch { } if (Convert.ToBoolean(ConfigurationManager.AppSettings["UseDoubleAutentificationForLogin"])) { if (TempData["tempLogin"] == null) { response r = ur.Login(model.Username, model.Password); if (r.Result != null) { TempData["tempLogin"] = (Utilizator)r.Result; string _to = ((Utilizator)r.Result).EMAIL; //string rnd = "1234"; // pt. test Random generator = new Random(); string rnd = generator.Next(0, 1000000).ToString("D6"); SendVerificationCode(EmailProfiles.AwsCereriSES, _to, rnd); TempData["verificationCode"] = rnd; TempData["verificationCodeIssueTime"] = DateTime.Now; return(RedirectToAction("Login", "Utilizatori", new { returnUrl = returnUrl })); } else { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.FAILED_LOGIN); return(View(model)); } } else { if (TempData["verificationCode"] == null || String.IsNullOrWhiteSpace(TempData["verificationCode"].ToString())) // a expirat codul! TO DO !!! { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_EXPIRED); //TempData.Clear(); TempData.Keep(); return(View(model)); //return RedirectToAction("Login", "Utilizatori", new { returnUrl = returnUrl }); } else { var passedSeconds = (DateTime.Now - (DateTime)TempData["verificationCodeIssueTime"]).TotalSeconds; if (Math.Abs((int)passedSeconds) > Convert.ToInt32(ConfigurationManager.AppSettings["VerificationCodeExpiration"])) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_EXPIRED); //TempData.Clear(); TempData.Keep(); return(View(model)); //return RedirectToAction("Login", "Utilizatori", new { returnUrl = returnUrl }); } else { if (String.IsNullOrEmpty(model.Code)) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.EMPTY_CODE); return(View(model)); } else { if (model.Code != TempData["verificationCode"].ToString()) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_DONT_MATCH); return(View(model)); } else { return(LoginInternal(returnUrl)); } } } } } } else { response r = ur.Login(model.Username, model.Password); if (r.Result != null) { TempData["tempLogin"] = (Utilizator)r.Result; return(LoginInternal(returnUrl)); } else { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.FAILED_LOGIN); return(View(model)); } } }
public ActionResult TokenLogin(LoginJson model, string submitCode) { if (submitCode == "Retrimite codul") { model.Code = null; //string rnd = "1234"; // pt. test Utilizator u = (Utilizator)TempData["tempLogin"]; string _token = TempData["TOKEN"].ToString(); string _url = TempData["URL"].ToString(); string[] separator = { "|" }; string[] token = _token.Split(separator, StringSplitOptions.RemoveEmptyEntries); int id = Convert.ToInt32(token[1]); string conStr = HttpContext.Server.MapPath("~").ToLower().IndexOf("test") > 0 ? CommonFunctions.StringCipher.Decrypt(System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString_test"].ConnectionString, CommonFunctions.StringCipher.RetrieveKey()) : CommonFunctions.StringCipher.Decrypt(System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString, CommonFunctions.StringCipher.RetrieveKey()); // separam socisa de socisa_test Dosar d = new Dosar(Convert.ToInt32(u.ID), conStr, id); SocietateAsigurare sa = (SocietateAsigurare)d.GetSocietateRca().Result; Random generator = new Random(); string rnd = generator.Next(0, 1000000).ToString("D6"); SendVerificationCode(EmailProfiles.AwsCereriSES, sa.EMAIL_NOTIFICARI, rnd); TempData.Clear(); TempData["TOKEN"] = _token; TempData["URL"] = _url; TempData["tempLogin"] = u; TempData["verificationCode"] = rnd; TempData["verificationCodeIssueTime"] = DateTime.Now; return(View("TokenLogin", new LoginJson())); } if (TempData["TOKEN"] != null && TempData["URL"] != null && TempData["tempLogin"] != null) { if (TempData["verificationCode"] == null || String.IsNullOrWhiteSpace(TempData["verificationCode"].ToString())) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_EXPIRED); //TempData.Clear(); TempData.Keep(); return(View("TokenLogin", model)); } else { //model.Code = TempData["verificationCode"].ToString(); var passedSeconds = (DateTime.Now - (DateTime)TempData["verificationCodeIssueTime"]).TotalSeconds; if (Math.Abs((int)passedSeconds) > Convert.ToInt32(ConfigurationManager.AppSettings["VerificationCodeExpiration"])) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_EXPIRED); //TempData.Clear(); TempData.Keep(); return(View("TokenLogin", model)); } else { if (String.IsNullOrEmpty(model.Code)) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.EMPTY_CODE); return(View("TokenLogin", model)); } else { if (model.Code != TempData["verificationCode"].ToString()) { ModelState.AddModelError("", socisaV2.Resources.ErrorMessagesResx.VERIFICATION_CODE_DONT_MATCH); return(View("TokenLogin", model)); } else { return(InternalTokenLogin(TempData["TOKEN"].ToString(), TempData["URL"].ToString(), (Utilizator)TempData["tempLogin"])); } } } } } else { TempData.Clear(); return(Redirect("~")); } }