Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddDistributedSqlServerCache not saving session into database

Im trying in save HttpContext.Session into Database instead of server of memmory

problem is nothing is saved in table and session always gets null.

this is my start up Code :

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDistributedSqlServerCache(options =>
        {
            options.ConnectionString = @"Data Source=localhost;Initial Catalog=mydb;Integrated Security=True;";
            options.SchemaName = "dbo";
            options.TableName = "SQLSessions";
        });

        services.AddSession(options => {
            options.CookieName = "Test.Session";
            options.IdleTimeout = TimeSpan.FromMinutes(60);
        });

    }


   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDistributedCache cache)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseSession();

        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
        app.UseHttpMethodOverride();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}"
                );
            routes.MapRoute(
                name: "AdvertisementImages",
                template: "{controller=Home}/{action=Images}/{type}/{name}", // URL with parameters
                defaults: new {type = 0, name = ""} // Parameter defaults
            );
        });

    }

and my controller code for saving session :

HttpContext.Session.SetObjectAsJson("TryToLogin", newLogin);

and my session Extension :

   public static class SessionExtensions
    {
        public static void SetObjectAsJson(this ISession session, string key, object value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T GetObjectFromJson<T>(this ISession session, string key)
        {
            var value = session.GetString(key);

            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }
    }

and this my session Table Schema :

enter image description here

like image 684
Ahad Porkar Avatar asked Sep 22 '26 02:09

Ahad Porkar


1 Answers

I recommend you read below tutorials:

  • Microsoft document about distributed cache
  • Configuring SQL Server For Session State In ASP.NET Core 1.0 MVC

I hope this helps ;)

like image 176
Yashar Aliabbasi Avatar answered Sep 24 '26 06:09

Yashar Aliabbasi