11
Manage Session state of ASP.NET Core 1.0 Application by SQL Server

Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Manage Session state of ASP.NET Core 1.0 Application by SQL Server

Page 2: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

What is session State ?

• ……is a mechanism that enables to store and retrieve user specific data that can be stored for visitor's session.

• discount level they are entitled, name , ID ,User role or data that is repeatedly required and need query a from database.

• stored in server memory, using persistent and/or distributed storage mechanisms (web farm etc.),in-memory.

Page 3: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Session management in ASP.NET Core 1.0

….. pluggable component, or "middleware" and is available in a Nuget package called Microsoft.AspNet.Session.

In-memory storage is also available as an optional package called Microsoft.Extensions.Caching.Memory.

Dependencies node of your project.json file:

"Microsoft.AspNet.Session": "1.0.0-rc1-final","Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final“

Page 4: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

How to User

Go to ConfigureServices method of the Startup class

(Startup.cs file). ConfigureServices method to register both caching and session with the services used in your application.

1. Enables to change the default options 2. Location for session cookies, 3. Default timeout period etc.

Page 5: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

How to Configur

Go to Configure method of the StartUp.cs file (register middleware)

1. Included packages for session management 2. tell the application to use the session management features

Then you add Session middleware to the application pipeline in the Configure method - ensuring that it appears before MVC:

Page 6: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

How to implement

Session values are serialisable for storage on remote servers. The new session framework stores items as byte arrays,

SetInt and SetString converts the ints and strings to a byte array.

Set other types on serialization need to by yourself :

BitConverter.ToBoolean(data, 0);

Also for reusability own extension methods on Isession can be developable .

Remove method delete individual values by key:

Context.Session.Remove("Name");

Page 7: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Use SQL Server instead of server memory as a store for session state :

1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide storage

2. Data is stored in a database and accessible from any server

3. Removes the load from the server's memory,

4. Data stored in database survives Application Pool recycles and server restarts.

Page 8: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Procedure :

1. Set up Database and Table

2. two ways in which you can generate the schema for the table so that it can be used by the Caching.SqlServer package.

A. Use a dnx command. Before you can do that, you need to install the command, the following command from a command prompt:which you do by executing

dnu commands install Microsoft.Extensions.Caching.SqlConfig

You should receive confirmation that the sqlservercache command was installed.

Once you have done that, you can execute the sqlservercachecommand that generates the appropriate table. It is in the format:sqlservercache create <connectionstring> <schema> <table>

Page 9: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Procedure :

The alternative approach to table creation is to execute the following CREATE TABLE script in SQL Server Management Studio against your chosen database:

CREATE TABLE [dbo].[Sessions](

Id nvarchar(900) COLLATE

SQL_Latin1_General_CP1_CS_AS NOT NULL,

Value varbinary(MAX) NOT NULL,

ExpiresAtTime datetimeoffset NOT NULL,

SlidingExpirationInSeconds bigint NULL,

AbsoluteExpiration datetimeoffset NULL,

CONSTRAINT pk_Id PRIMARY KEY (Id));

CREATE NONCLUSTERED INDEX Index_ExpiresAtTime ON

[dbo].[Sessions](ExpiresAtTime);

add a connection string to your appSettings.json file if you haven't already done so:

Page 10: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Then you add Session middleware to the application pipeline in the Configure method - ensuring that it appears before MVC:

add Session middleware to the application pipeline in the Configure method - ensuring that it appears before MVC:

Page 11: Manage Session state of ASP.NET Core 1.0 Application by ...€¦ · 1. In ASP.NET Core 1.0, there is no default storage for this data. You have to explicitly choose a package to provide

Thanks