public void validationTokenTime(string userName = "", string connection = "") { try { var token = DynamicToken.token[userName.Trim()].token; var validTime = DateTime.Now; validTime = validTime.AddMinutes(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); TokenDateViewModel model = new TokenDateViewModel() { token = token, validationToken = validTime }; DynamicToken.token[userName.Trim()] = model; Clients.Client(connection).alertToken("تمدید زمان سشن با موفقیت انجام شد"); } catch (Exception) { } }
public string login(LoginViewModel login) { string res = "1"; UnitOfWork u = new UnitOfWork(); var allowLog = u.Setting.GetRecord(1).ActiveLog; if (!User.Identity.IsAuthenticated) { if (!string.IsNullOrEmpty(login.userName)) { if (!string.IsNullOrEmpty(login.password)) { if (ModelState.IsValid) { var user = u.User.Get(p => p.Username == login.userName).FirstOrDefault(); if (user != null) { var pas = FormsAuthentication.HashPasswordForStoringInConfigFile(login.password, "MD5"); if (user.Remove) { res = "کاربری با این مشخصات یافت نشد"; } else if (user.IsActive) { if (pas != user.Password) { res = "نام کاربری و یا کلمه عبور اشتباه می باشد"; user.WrongPassword += 1; if (user.WrongPassword >= 5) { user.WrongPassword = 5; user.IsActive = false; } u.User.Update(user); u.User.Save(); } else { string userName = login.userName + "،" + FormsAuthentication.HashPasswordForStoringInConfigFile(HttpContext.Request.UserHostAddress, "MD5"); if (login.rememberMe == "on") { FormsAuthentication.SetAuthCookie(userName, true); } else { FormsAuthentication.SetAuthCookie(userName, false); } if (allowLog) { Logs log = new Logs() { UserId = user.ID, LogDate = DateTime.Now, Description = "ورود به پنل مدیریت دفترچه تلفن" }; u.Log.Insert(log); } IPs iP = new IPs() { Remove = false, IP = HttpContext.Request.UserHostAddress, UserId = user.ID }; var ips = u.IP.Get(p => p.IP == iP.IP && p.UserId == user.ID && p.Remove == false).FirstOrDefault(); if (ips == null) { u.IP.Insert(iP); u.Log.Save(); } var validTime = DateTime.Now; validTime = validTime.AddMinutes(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); TokenDateViewModel tokenDateView = new TokenDateViewModel() { token = Guid.NewGuid().ToString(), validationToken = validTime }; DynamicToken.token[login.userName] = tokenDateView; HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = tokenDateView.token + "~" + tokenDateView.validationToken.ToString(); StudentCookies.Expires = DateTime.Now.AddMinutes(Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); Response.Cookies.Add(StudentCookies); } } else { res = "کاربر گرامی حساب کاربری شما غیر فعال می باشد "; } } else { res = "کاربری با این مشخصات یافت نشد"; } } } else { res = "لطفا کلمه عبور را وارد نمایید"; } } else { res = "لطفا نام کاربری را وارد نمایید"; } } return(res); }
void Application_AcquireRequestState(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) { var context = new HttpContextWrapper(Context); var token = Request.Cookies["token"]; var user = Tools.GetFullUser(User.Identity.Name); if (token != null && user != null) { var t = new TokenDateViewModel(); if (DynamicToken.token.ContainsKey(user.Username)) { t = DynamicToken.token[user.Username]; } if (!string.IsNullOrEmpty(t.token) && t.validationToken != null) { var tok = token.Value.ToString().Split('~'); if (tok.Length == 2) { if (t.token.Trim() != tok[0].ToString().Trim()) { UnitOfWork u = new UnitOfWork(); Logs log = new Logs() { UserId = user.ID, LogDate = DateTime.Now, Description = "ورود به حساب کابری با اکانت " + user.Username + " که از قبل فعال بوده است" }; u.Log.Insert(log); u.Log.Save(); FormsAuthentication.SignOut(); if (System.Configuration.ConfigurationManager.AppSettings["removeAllToken"] == "true") { var validTime = DateTime.Now; validTime.AddMinutes(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); TokenDateViewModel model = new TokenDateViewModel() { token = Guid.NewGuid().ToString(), validationToken = validTime }; DynamicToken.token[user.Username] = model; HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = "temp~temp"; StudentCookies.Expires = DateTime.Now.AddMinutes((-1) * Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); Response.Cookies.Add(StudentCookies); } if (!context.Request.IsAjaxRequest()) { Context.Response.Clear(); Response.Redirect("/?id=u"); } else { Response.Clear(); Response.Redirect("/temp"); } } if (t.validationToken <= DateTime.Now) { UnitOfWork u = new UnitOfWork(); Logs log = new Logs() { UserId = user.ID, LogDate = DateTime.Now, Description = "اتمام زمان مجاز استفاده از سشن برای کاربر " + user.Username }; u.Log.Insert(log); u.Log.Save(); FormsAuthentication.SignOut(); if (System.Configuration.ConfigurationManager.AppSettings["removeAllToken"] == "true") { var validTime = DateTime.Now; validTime.AddMinutes(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); TokenDateViewModel model = new TokenDateViewModel() { token = Guid.NewGuid().ToString(), validationToken = validTime }; DynamicToken.token[user.Username] = model; HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = "temp~temp"; StudentCookies.Expires = DateTime.Now.AddMinutes((-1) * Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); Response.Cookies.Add(StudentCookies); } if (!context.Request.IsAjaxRequest()) { Context.Response.Clear(); Response.Redirect("/?id=timeOut"); } else { Response.Clear(); Response.Redirect("/timeOut"); } } } else { HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = DynamicToken.token[user.Username].token + "~" + DynamicToken.token[user.Username].validationToken.ToString(); StudentCookies.Expires = DynamicToken.token[user.Username].validationToken; Response.Cookies.Add(StudentCookies); } } else { UnitOfWork u = new UnitOfWork(); Logs log = new Logs() { UserId = user.ID, LogDate = DateTime.Now, Description = "خروج کاربر توسط سیستم به دلیل وجود دو کاربر هم زمان با نام کاربری " + user.Username }; u.Log.Insert(log); u.Log.Save(); FormsAuthentication.SignOut(); if (System.Configuration.ConfigurationManager.AppSettings["removeAllToken"] == "true") { var validTime = DateTime.Now; validTime.AddMinutes(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); TokenDateViewModel model = new TokenDateViewModel() { token = Guid.NewGuid().ToString(), validationToken = validTime }; DynamicToken.token[user.Username] = model; HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = "temp~temp"; StudentCookies.Expires = DateTime.Now.AddMinutes((-1) * Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["validTokenTime"].ToString())); Response.Cookies.Add(StudentCookies); } if (!context.Request.IsAjaxRequest()) { Context.Response.Clear(); Response.Redirect("/?id=u"); } else { Response.Clear(); Response.Redirect("/temp"); } } } else { try { HttpCookie StudentCookies = new HttpCookie("token"); StudentCookies.Value = DynamicToken.token[user.Username].token + "~" + DynamicToken.token[user.Username].validationToken.ToString(); StudentCookies.Expires = DynamicToken.token[user.Username].validationToken; Response.Cookies.Add(StudentCookies); } catch (Exception) { } } } }