/// <summary> /// Saves test data /// </summary> /// <param name="data">Session data</param> /// <param name="sessionId">Session id (needed if data stores in database)</param> /// <param name="saveToCookie">'True' to make data stored in cookie</param> private void SetTestData(TestData data, int sessionId = 0, bool saveToCookie = false) { string dataString = XmlManager.SerializeObjectUTF8(data); string tokenString; string testInfoString; if (saveToCookie) { testInfoString = HttpUtility.UrlEncode(dataString); tokenString = MD5HashManager.GenerateKey(testInfoString); } else { tokenString = MD5HashManager.GenerateKey(dataString); testInfoString = TestSessionManager.Write(dataString, sessionId).ToString(CultureInfo.InvariantCulture); } var testInfo = new HttpCookie("TestInfo", testInfoString); var securityToken = new HttpCookie("SecurityToken", tokenString); Response.Cookies.Add(testInfo); Response.Cookies.Add(securityToken); TempData["testInfo"] = testInfo; TempData["securityToken"] = securityToken; }
/// <summary> /// Returns test data /// </summary> /// <param name="databaseRecordId">Returns ID for records are storing in Data Base</param> /// <param name="getFromCookie">'True' to get data from cookie rather then database</param> /// <returns></returns> private TestData GetTestData(out int databaseRecordId, bool getFromCookie = false) { var testInfoCookie = (HttpCookie)TempData["TestInfo"]; var securityTokenCookie = (HttpCookie)TempData["SecurityToken"]; databaseRecordId = 0; if (testInfoCookie == null || securityTokenCookie == null || string.IsNullOrWhiteSpace(testInfoCookie.Value) || string.IsNullOrWhiteSpace(securityTokenCookie.Value)) { testInfoCookie = Request.Cookies["TestInfo"]; securityTokenCookie = Request.Cookies["SecurityToken"]; if (testInfoCookie == null || securityTokenCookie == null || string.IsNullOrWhiteSpace(testInfoCookie.Value) || string.IsNullOrWhiteSpace(securityTokenCookie.Value)) { return(null); } } else { Response.Cookies.Add(testInfoCookie); Response.Cookies.Add(securityTokenCookie); } string data = testInfoCookie.Value; string securityToken = securityTokenCookie.Value; if (getFromCookie) { data = HttpUtility.UrlDecode(data); } else { var id = 0; try { id = int.Parse(testInfoCookie.Value); } catch (Exception) { return(null); } databaseRecordId = id; data = TestSessionManager.Read(id); } if (MD5HashManager.GenerateKey(data) != securityToken) { return(null); } return(XmlManager.DeserializeObject <TestData>(data)); }
/// <summary> /// Returns question hash /// </summary> /// <returns></returns> public string GetQuestionHash() { return(MD5HashManager.GenerateKey(CurrentQuestionId + TestSeed)); }