1: Introduction to Session
What is Session? Simply put, it is a number given to the client by the server. When a web server is running, there may be several users browsing websites that are running on that server. When each user first connects to the web server, he establishes a session with the server, and the server automatically assigns it a SessionID to identify the user's unique identity. This SessionID is a 24-character string randomly generated by the web server, and we'll see it in action in the experiment below.
2: Centralized mode and configuration of the Session in the Asp.Net
(1): InProc, Session is stored in the process, which is also the storage method in the ASP.Net and is also the default value (2): StateServer, Session is stored in a separate state service, i.e., ASP.NET State Service (3): SQLServer, Session is stored in the SQL Server database (4): Custom, a custom save method, which can be saved in Oracle, SQLServer, MongoDb, and Redis This article mainly talks about the storage and sharing of Sessions in Redis
3: Session Provider configuration based on Redis
(1) Make the following changes to the HomeControllers of Sites A and B, as shown in the figure below, and add the functions of creating and obtaining sessions
The corresponding View views
(2) RedisProvider configuration: Microsoft.Web.RedisSessionStateProvider Install Microsoft.Web.RedisSessionStateProvider with Nuget as shown in the following image:
Modify the profile:
4. Load balancing session test
(1) Enter in the browser:http://192.168.152.128/ Create a Session
(2) Enter in the browser:http://192.168.152.128/home/getsessionvalue, Open two tabs, be sure to open them in the same browser window, otherwise the SessionID will definitely be different
From the figure, you can see that site A shares the sessions created by site B. You can also see the Session you just created from redis as shown in the figure below
|