public void RegenerateSessionID() { System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState.SessionIDManager(); string oldId = manager.GetSessionID(Context); string newId = manager.CreateSessionID(Context); bool isAdd = false, isRedir = false; manager.SaveSessionID(Context, newId, out isRedir, out isAdd); }
public static string GetSessionId() { string sessionId = string.Empty; try { System.Web.SessionState.SessionIDManager Manager = new System.Web.SessionState.SessionIDManager(); sessionId = Manager.CreateSessionID(HttpContext.Current); } catch (Exception ex) { sessionId = "N/A"; } return(sessionId); }
public static void Test() { // http://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console System.Console.OutputEncoding = System.Text.Encoding.UTF8; System.Web.SessionState.SessionIDManager man = new System.Web.SessionState.SessionIDManager(); for (int i = 0; i < 100; ++i) { string sessid = man.CreateSessionID(null); int num = CountNumeric(sessid); System.Console.WriteLine(num.ToString() + ": " + sessid); string mySessId = RandomPassword(new SessionUidOptions()); System.Console.WriteLine(mySessId); // string pw = CreatePassword(8); // string pw = RandomPassword(new PasswordOptions()); // string pw = RandomPassword(new CyrillicPasswordOptions()); //string pw = RandomPassword(new GenericPasswordOptions() //{ // PASSWORD_CHARS_LCASE = "æaàâäbcçdeéêëfgiïîjklmnoœôöpqrstuùûüüvwxyÿz", // PASSWORD_CHARS_UCASE = "ÆAÀÂÄBCÇDEÉÊËFGHIÏÎJKLMNOŒÔÖPQRSTUÙÛÜÜVWXYŸZ", // PASSWORD_CHARS_NUMERIC = "0123456789", // PASSWORD_CHARS_SPECIAL = "*-+=_&!?$€£%{}()[]/\\|.:;", // NumberOfLowerCaseCharacters = 5, // NumberOfUpperCaseCharacters = 1, // NumberOfNumericCharacters = 3, // NumberOfSpecialCharacters = 1 //}); // string pw = RandomPassword(new SafePasswordOptions()); // System.Console.WriteLine(pw); } }
/// <summary> /// Method that creates a new session and touches it right before the expiry a couple of times. /// The final time it will not wait until after the expiration which the method will then /// expect the session to no longer be available. /// </summary> /// <param name="provider">The MongoDB provider to use for the test.</param> private void VerifySessionSlidingExpiration(MongoDBSessionStateProvider provider) { HttpRequest request = null; HttpResponse response = null; HttpContext context = GetContext(out request, out response); var sessionId = _SessionIdManager.CreateSessionID(context); var dataStore = provider.CreateNewStoreData(context, (_TimeoutInSeconds / 60)); dataStore.Items["Dummy"] = "Value"; TestContext.WriteLine("Creating dummy session with id {0}", sessionId); provider.SetAndReleaseItemExclusive(context, sessionId, dataStore, null, true); int iterations = 4; for (int i = 0; i < iterations; i++) { bool isLastIteration = (i == iterations - 1); int counter = _TimeoutInSeconds + (isLastIteration ? 10 : -10); TestContext.WriteLine("Waiting {0} seconds (expiration set to {1} seconds)...", counter, _TimeoutInSeconds); while (counter > 0) { System.Threading.Thread.Sleep(1000); counter--; } TestContext.WriteLine("Retrieving session again to reset expiry"); bool locked; TimeSpan lockAge; object lockId; System.Web.SessionState.SessionStateActions actions; var dataStore2 = provider.GetItemExclusive(context, sessionId, out locked, out lockAge, out lockId, out actions); if (isLastIteration) { if (dataStore2 != null) { Assert.Fail("Session has NOT expired."); } ISessionStateData dataFromCache; bool existsInCache = CheckSessionExistsInCache(provider, sessionId, out dataFromCache); if (existsInCache) { Assert.Fail("Session has expired however it still exists in the memory cache."); } } else { if (dataStore2 == null || dataStore2.Items.Count == 0) { Assert.Fail("Session Missing prior to expiry??"); } else { TestContext.WriteLine("Session retrieved successfully during iteration {0}", (i + 1)); } } if (lockId != null) { provider.ReleaseItemExclusive(context, sessionId, lockId); } } }