4

I've written a ASP.Net Core application that uses SQL based session storage. When I run the application via visual studio, the session object persists builds and starting/stopping IIS express, however, when I deploy it to the server (Win server 2012R2), an app pool recycle causes it to lose session state. - Edit, actually a restart of my computer, which obviously ends an underlying process, causes me to lose session in visual studio also.

My ConfigureServices:

        // Serializer settings have been changed so that the JSON response sends the same case, not lower case first letter as default
        services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

        // Builder to access the config
        var builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());
        builder.AddJsonFile("appsettings.json");
        var config = builder.Build();            

        // Get and configure the session timeout
        int SessionTimeout = Convert.ToInt32(config["AppSettings:SessionTimeout"]);
        services.AddSession(s =>
        {
            s.IdleTimeout = TimeSpan.FromMinutes(SessionTimeout);                
        }
        );

        // Get the session state datebase location
        var AspStateConnectionString = config["AppSettings:ConnectionStrings:ASPStateConnectionString"];
        services.AddDistributedSqlServerCache(o =>
        {
            o.ConnectionString = AspStateConnectionString;
            o.SchemaName = "dbo";
            o.TableName = "Sessions";
        });

My Configure:

        // Configure to use session
        app.UseSession();

        // Pass context to CoreSessionManager class and ContextPerRequest
        var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
        CoreSessionManager.Configure(httpContextAccessor);
        CoreLibrary.ContextPerRequest.Configure(httpContextAccessor);

        // Configure the routes - defaults to login page.
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Account}/{action=Login}");
        });

The CoreSessionManager class does the work around the actual session objects:

    private static IHttpContextAccessor HttpContextAccessor;
    public static void Configure(IHttpContextAccessor httpContextAccessor)
    {
        HttpContextAccessor = httpContextAccessor;
    }


    public static UserDetail UserDetail
    {
        get
        {
            if (HttpContextAccessor.HttpContext.Session.Get("UserDetail") != null)
                return JsonConvert.DeserializeObject<UserDetail>(HttpContextAccessor.HttpContext.Session.GetString("UserDetail"));

            return null;
        }
        set
        {
            HttpContextAccessor.HttpContext.Session.SetString("UserDetail", JsonConvert.SerializeObject(value));
        }
    }

If this UserDetails is null, the user is kicked out to the login screen. Is there something I am missing? Do I need to add in some other configurations?

Thanks in advance, David

1
  • Seems to be correct. Commented Dec 20, 2016 at 12:42

2 Answers 2

1

Did you setup ASPState on your sql server?

This link might help you to setup ASPState.

Or follow this steps:

Open a command prompt and locate the following path:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 based on your OS version and .NET version Use the following statement:

when using default ASPState database and SQL security, type the command below:

aspnet_regsql -S serverName -U UserName -P Password -ssadd -sstype p

Using default ASPState database and windows security, type the command below:

aspnet_regsql -S serverName -E -ssadd -sstype p

Using custom database and SQL security, type the command below:

aspnet_regsql -d TableName -S serverName -U UserName -P Password -ssadd -sstype c
Sign up to request clarification or add additional context in comments.

2 Comments

I did. I ran the SQL command directly as per this link: [link(]mikesdotnetting.com/article/292/…) I can see the entries populating and when I change the login string, the sessions don't create.
Also, I may be wrong, but I believe that is for creating the old version of the ASPState tables. It has a different structure in ASP.Net Core.
0

The reason this wasn't working was because I had to enable Load User Profile in the application pool in IIS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.