Skip to content

User Session Manager Middleware

Si Carter edited this page Oct 13, 2018 · 1 revision

User Session Manager Middleware

The User Session Manager middleware plugin is used to add user session data to the HttpContext object passed to controllers.

Settings and Configuration

The User Session Manager can be configured using appsettings.json. Add an entry named UserSessionSettings.

StaticFileExtensions

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;

CookieName

Name of session cookie that will be added to the user session. This is used to uniquely identify the visitor.

EncryptionKey

String, key used to encrypt the session id within the cookie. Default value is: Dfklaosre;lnfsdl;jlfaeu;dkkfcaskxcd3jf

SessionTimeout

Uint, number of minutes before session data expires, default 25 minutes. Minimum 15 minutes, maximum 200 minutes.

Access Session Data

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();
    }

UserSession class

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.

UserSession Properties

The following properties are available within the UserSession class.

CountryCode

Visitors country code, 2 digit country identifier. Default ZZ is unknown.

Region

Visitors Region

CityName

Name of visitors city, where the Ip address is located.

Latitude { get; protected set; }

Latitude of visitor, where Ip address is located.

Longitude { get; protected set; }

Longitude of visitor, where Ip address is located.

UserID { get; set; }

Unique Id to identify user. This value is set using the host application after login.

UserName

Name of user. This value is set using the host application after login.

UserEmail

Users email address. This value is set using the host application after login.

MobileManufacturer

Not used at present.

MobileModel

Not used at present.

ScreenWidth

Not used at present.

ScreenHeightNot used at present.

Pages

Lists all pages that the user has visited as part of the session.

TotalTime

Total time the user has spent browsing, in minutes.

CurrentPage

Current page the user is visiting.

CurrentSale

Value of sale for current visitor. This is set in the host application after a sale is made.

CurrentSaleCurrency

Currency used for the current sale for the visitor. This is set in the host application after a sale is made.

Tag

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.

CityID

Id of the city where the Ip address is located.

IsBot

Identifies the session as a Bot/Spider or not.

Bounced

Indicates that the session was created, but only one page was visited during the visit.

InitialReferrer

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
}

Created

Date/time session was created.

SaveStatus

Indicates whether the session is saved or not.

SessionID

Unique session identifier.

InternalSessionID

Value used to identify the session after it has been saved.

IPAddress

Visitors Ip Address

Status

Current status

UserAgent

Visitors user agent.

IsMobileDevice

Indicates whether the session is on a mobile device or not.

IsBrowserMobile

Indicates whether the browser is on a mobile device or not.

MobileRedirect

User defined value to indicate whether the session is currently being redirected into a mobile version of the host application or not.

Referal

Referral Status.

HostName

Visitors Host Name.

PageSaveStatus

Page save status.

#UserSession Methods The UserSession exposes several methods for managing sessions.

Clone

Clones the session.

Login

After login, set's the basic user details for a session.

Sale

After a sale has been made, set's the sale details for a session.

Obtaining Geo Ip Data

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.

IGeoIpDataService

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

UserSession management takes place within it's own highly optimised thread and does not interfere with the Main process thread.

Saving Session Data and Page View Data

The SharedPluginFeatures module defines an interface called IUserSessionService, this exposes the following methods that allow a host application to save session data.

Closing

The user session is about to be closed after reaching the time out value.

Created

The user session has been created. This method can be used to attach custom, application specific data to the UserSession.Tag property.

Retrieve

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.

Save

The session needs saving. Check save status prior to saving the session. Once saved, change the value to Saved.

SavePage

The session page data needs saving. Check save status prior to saving. Once saved, change the value to Saved.