-
Notifications
You must be signed in to change notification settings - Fork 19
User Session Manager Middleware
The User Session Manager middleware plugin is used to add user session data to the HttpContext object passed to controllers.
The User Session Manager can be configured using appsettings.json. Add an entry named UserSessionSettings.
String, list of static file extensions that will not have Session data appended to the HttpContext object. Items must be separated using semi colon, i.e. .less;.ico;.css;.js;.svg;.jpg;.jpeg;.gif;.png;.eot;
Name of session cookie that will be added to the user session. This is used to uniquely identify the visitor.
String, key used to encrypt the session id within the cookie. Default value is: Dfklaosre;lnfsdl;jlfaeu;dkkfcaskxcd3jf
Uint, number of minutes before session data expires, default 25 minutes. Minimum 15 minutes, maximum 200 minutes.
Session data can be accessed within controllers by retrieving the user session data from HttpContext:
public IActionResult Index()
{
UserSession session = (UserSession)HttpContext.Items["UserSession"];
ViewBag.Username = String.IsNullOrEmpty(session.UserName) ? "Guest" : session.UserName;
return View();
}
The UserSession class has many default properties which can be used to help track user sessions. See https://sicarterblog.wordpress.com/category/user-session/ for further information on the class and potential uses.
The following properties are available within the UserSession class.
Visitors country code, 2 digit country identifier. Default ZZ is unknown.
Visitors Region
Name of visitors city, where the Ip address is located.
Latitude of visitor, where Ip address is located.
Longitude of visitor, where Ip address is located.
Unique Id to identify user. This value is set using the host application after login.
Name of user. This value is set using the host application after login.
Users email address. This value is set using the host application after login.
Not used at present.
Not used at present.
Not used at present.
Lists all pages that the user has visited as part of the session.
Total time the user has spent browsing, in minutes.
Current page the user is visiting.
Value of sale for current visitor. This is set in the host application after a sale is made.
Currency used for the current sale for the visitor. This is set in the host application after a sale is made.
This property is a user defined value or class instance that can be attached to the session allowing for a more personalised visit throught the host application.
Id of the city where the Ip address is located.
Identifies the session as a Bot/Spider or not.
Indicates that the session was created, but only one page was visited during the visit.
Indicates the referrer for the session, this is one of the following Enum values:
public enum ReferalType
{
Unknown = 0,
Direct = 1,
Organic = 2,
Referal = 3,
Facebook = 4,
Twitter = 5,
Google = 6,
Yahoo = 7,
Bing = 8
}
Date/time session was created.
Indicates whether the session is saved or not.
Unique session identifier.
Value used to identify the session after it has been saved.
Visitors Ip Address
Current status
Visitors user agent.
Indicates whether the session is on a mobile device or not.
Indicates whether the browser is on a mobile device or not.
User defined value to indicate whether the session is currently being redirected into a mobile version of the host application or not.
Referral Status.
Visitors Host Name.
Page save status.
#UserSession Methods The UserSession exposes several methods for managing sessions.
Clones the session.
After login, set's the basic user details for a session.
After a sale has been made, set's the sale details for a session.
It is down to the host application to obtain Geo Ip data for the session, there are a variety of sources for this on the internet. The SharedPluginFeatures module defines an IGeoIpDataService inteface, the host application should implement this interface which will then be called automatically during session initialisation.
This interface exposes 1 method called GetIPAddressDetails, this allows the host application to obtain Geo Ip data from the Geo Ip service of their choice.
UserSession management takes place within it's own highly optimised thread and does not interfere with the Main process thread.
The SharedPluginFeatures module defines an interface called IUserSessionService, this exposes the following methods that allow a host application to save session data.
The user session is about to be closed after reaching the time out value.
The user session has been created. This method can be used to attach custom, application specific data to the UserSession.Tag property.
The session needs to be retrieved. This option should only be called if implemented within a web farm. It indicates that the session was started elsewhere but that the value needs to be retrieved to continue.
The session needs saving. Check save status prior to saving the session. Once saved, change the value to Saved.
The session page data needs saving. Check save status prior to saving. Once saved, change the value to Saved.