/// <summary> /// Ensures a specific key to be either already in the ASP.NET MVC session state or to be newly created /// </summary> /// <typeparam name = "T">The generic type to be returned</typeparam> /// <param name = "state">The session state.</param> /// <param name = "key">The session state key.</param> /// <returns>The session state value.</returns> /// <example> /// <code> /// public List<string> StringValues { /// get { return this.Session.Ensure<List<string>>("StringValues"); } /// set { this.ViewState.Set("StringValues", value); } /// } /// </code> /// </example> /// <remarks> /// Contributed by Michael T, http://about.me/MichaelTran /// </remarks> public static T Ensure <T>(this HttpSessionStateBase state, string key) where T : class, new() { var value = state.Get <T>(key); if (value == null) { value = new T(); state.Set(key, value); } return(value); }
public static void SetRoleText(this HttpSessionStateBase state, string roles) { state.Set(KEY_ROLE_TEXT, roles); }
public static void SetRoleIds(this HttpSessionStateBase state, int[] roleIds) { state.Set(KEY_ROLEID_LIST, roleIds); }
public static void SetUserDetail(this HttpSessionStateBase state, UserDetails usrDetails) { state.Set(KEY_USER_DETAIL, usrDetails); }
public static void SetUser(this HttpSessionStateBase state, User usr) { state.Set(KEY_USER_ID, usr.Id); state.Set(KEY_USER_NAME, usr.UserName); }