/// <summary>
        /// Saves  the M main object into the database.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="httpContext">The HTTP context.</param>
        /// <param name="main">The main.</param>
        public static void SaveDatabase(IConfiguration configuration, HttpContext httpContext, object main)
        {
            var  storageID = GetStorageID(main.GetType().Name);
            Guid session   = Guid.NewGuid(); // cannot exist in the database
            var  newCookie = new NameValueCollection();
            var  cookie    = httpContext.Request.Cookies[storageID].FromCookieString();

            if (cookie != null)
            {
                Guid.TryParse(cookie["session"], out session);
            }
            Func <byte[], byte[]> filter = null;

            if (StorageImplementation.GetEncryptDatabaseStorage(configuration))
            {
                var key    = (cookie["key"] != null) ? Convert.FromBase64String(cookie["key"]) : null;
                var secret = StorageImplementation.GetSecret(key);
                filter           = x => Crypt.Encrypt(secret, x);
                newCookie["key"] = Convert.ToBase64String(secret.Key);
            }
            using (var db = new ASP_DBEntities())
            {
                var savedSession = db.SaveMain(main.GetType(), StorageImplementation.Bytes(main, filter), session);
                newCookie["session"] = savedSession.ToString();
            }

            var days    = configuration.GetValue <int>("DatabaseStorageExpires");
            var options = new CookieOptions()
            {
                Expires  = DateTime.Now.AddDays(days),
                HttpOnly = true,
                SameSite = SameSiteMode.Strict
            };

            httpContext.Response.Cookies.Append(storageID, newCookie.ToCookieString(), options);
        }
Exemple #2
0
 /// <summary>
 /// Recreates the Main object from the ViewState and copies members
 /// used on the client side with the overridden ExposeMembers().
 /// Returns a new instance if no ViewState is given
 /// </summary>
 /// <param name="filter">The filter.</param>
 public virtual void DeserializeMain(Func <byte[], byte[]> filter = null)
 {
     this.Main  = StorageImplementation.LoadFromViewstate(() => new TModel(), this.ViewState, filter);
     this.IsNew = false;
 }
Exemple #3
0
 /// <summary>
 /// Internally serializes the Main object into the ViewState.
 /// </summary>
 /// <param name="filter">The filter.</param>
 public void SerializeMain(Func <byte[], byte[]> filter = null)
 {
     this.ViewState = StorageImplementation.ViewState(this.Main, filter);
 }
        /// <summary>
        /// Saves the M main object into the session.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="httpContext">The HTTP context.</param>
        /// <param name="main">The main.</param>
        public static void SaveSession(IConfiguration configuration, HttpContext httpContext, object main)
        {
            var storageID = GetStorageID(main.GetType().Name);

            httpContext.Session.Set(storageID, StorageImplementation.Bytes(main));
        }