示例#1
0
        /// <summary>
        /// Testable SaveMain
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="db">The database.</param>
        /// <param name="instance">The instance.</param>
        /// <param name="session">The session.</param>
        /// <param name="filter">encrypt filter</param>
        /// <returns></returns>
        public static Guid SaveMain <M>(ASP_DBEntities db, M instance, Guid?session, Func <byte[], byte[]> filter = null)
            where M : class
        {
            var query = from m in db.Main
                        where m.session == session
                        select m;
            var main = query.FirstOrDefault();

            if (main == null)
            {
                main       = new Main();
                main.clsid = Clsid.Id(instance);
                db.Main.Add(main);      // INSERT
            }
            main.SetInstance(instance, filter);
            db.SaveChanges();
            return(main.session);  // get the new session guid set by the db on insert
        }
示例#2
0
        /// <summary>
        /// Returns the (unencrypted!) literal INSERT string of the loaded object
        /// for manually exporting session dumps.
        /// </summary>
        /// <returns>
        /// SQL INSERT string
        /// </returns>
        public string InsertSQL()
        {
            Trace.Assert(this.main != null, "Explicit serialization with SetInstance(controlStorage.Main) required beforehand");
            var clsid = Clsid.Id(this.mainInstance);    // throws if no Clsid attribute present

            // Let the future consumer SQL Server encode the string representation of the byte[]
            string hex   = String.Empty;
            var    query = "SELECT CONVERT(VARCHAR(MAX), @main, 1) AS [hex]";

            using (var db = new ASP_DBEntities())
            {
                var param = new SqlParameter("main", this.main);
                hex = db.Database.SqlQuery <String>(query, param).FirstOrDefault();
            }
            // Format according to get copy-pasted into Management Studio
            return(String.Format("INSERT INTO Main (clsid, main) SELECT '{0}', {1}\n" +
                                 "SELECT session FROM Main WHERE mainid = @@IDENTITY\n",
                                 clsid, hex));
        }
        public static (byte[] bytes, Func <byte[], byte[]> filter) DatabaseBytes(
            IConfiguration configuration, HttpContext httpContext, string storageID, Guid session)
        {
            byte[] bytes;
            Func <byte[], byte[]> filter = null;

            if (GetEncryptDatabaseStorage(configuration))
            {
                var keyString = httpContext.Request.Cookies[storageID].FromCookieString()["key"];
                var key       = (keyString != null) ? Convert.FromBase64String(keyString) : null;
                var secret    = GetSecret(key);
                filter = x => Crypt.Decrypt(secret, x);
            }
            using (var db = new ASP_DBEntities())
            {
                bytes = db.LoadMain(session);
            }
            return(bytes, filter);
        }
        /// <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);
        }