public static void CheckIfUSerIsAuthenticated() { ONyRDataSet.SysSessionDataTable sessions; if (HttpContext.Current.User.Identity.IsAuthenticated == false) { throw new ONyRException(ErrorCode.NoSessionError); } bool validSession = false; SysSessionTableAdapter sessionAdapter = new SysSessionTableAdapter(); sessions = sessionAdapter.GetDataByUserID(Convert.ToInt32(HttpContext.Current.Request.Cookies["UserId"].Value)); foreach (ONyRDataSet.SysSessionRow row in sessions) { if (row.SessionModified.AddMinutes(30).CompareTo(DateTime.Now) > 0) { validSession = true; } else { sessionAdapter.Delete1(row.ID); } } if (!validSession) { throw new ONyRException(ErrorCode.InvalidSessionError); } }
public static void CreationCookie(object sender, System.Web.ApplicationServices.CreatingCookieEventArgs e) { ONyRDataSet.SysUserDataTable users; ONyRDataSet.SysSessionDataTable sessions; ONyRDataSet.SysSessionRow session; SysUserTableAdapter userAdapter = new SysUserTableAdapter(); users = userAdapter.GetDataByUserName(e.UserName); if (users.Count == 0) { throw new ONyRException(ErrorCode.InvalidCredentialsError); } ONyRDataSet.SysUserRow user = users[0]; SysSessionTableAdapter sessionAdapter = new SysSessionTableAdapter(); sessions = sessionAdapter.GetDataByUserID(user.ID); foreach (ONyRDataSet.SysSessionRow row in sessions) { sessionAdapter.Delete1(row.ID); } sessionAdapter.CreateSession(user.ID); sessions = sessionAdapter.GetDataByUserID(user.ID); session = sessions[0]; sessionAdapter.Dispose(); userAdapter.UpdateLoginDate(user.ID); userAdapter.Dispose(); HttpContext.Current.Response.Cookies.Add(new HttpCookie("UserId", user.ID.ToString())); HttpContext.Current.Response.Cookies.Add(new HttpCookie("SessionId", session.ID.ToString())); }