This is a simple project that illustrates how to use the generic XSession class to handle session variables in aspnetcore web applications. This sample provides a generic class so any type of object can be stored as a session variable. To use the class, do the following
1. Define any class that corresponds to the data model you want to save in a session. Make sure the class is serializable and has a default constructor.
[Serializable] public class UserSessionVariables { public bool IsAuthenticated {get; set;} = false; public string UserID {get; set; } = ""; public UserSessionVariables() {} }
2. Add the following namespaces to all your controllers.
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters;
3. In all controllers, define a controller level variable to store the session.
PFSXSession<UserSessionVariables> session;
4. In each controller, define an OnActionExecuting function.
public override void OnActionExecuting(ActionExecutingContext context)
{// get the action<br> string action = this.ControllerContext.ActionDescriptor.ActionName.ToUpper(); // define a new user session session = new PFSXSession<PFSUserSessionVariables>(context.HttpContext); // now load the user session session.Load(AutoInitialize:true); bool bypass = false; // this is just simple login, login submit, and logout logic. It determines whether // we want to check the session authentication, etc. if (string.Compare(action, "LOGIN") == 0 || string.Compare(action, "LOGINSUBMIT") == 0 || string.Compare(action, "LOGOUT") == 0) { bypass = true; } if (!bypass) { // if user is not authenticated if (!session.SessionVariables.IsAuthenticated || session.IsExpired || session.IsCorrupt) { context.Result = new RedirectResult("/Home/Login"); } } // execute the base. base.OnActionExecuting(context);
}
5. In the ConfigureServices function in startup.cs, add the following to configure asp.net core to use session management and to store in server side memory.
public void ConfigureServices(IServiceCollection services) {// add server memory cache services.AddDistributedMemoryCache(); // add session services.AddSession();
}
6. In the Configure function in startup.cs, add the UseSession(); and make sure it is before the UseMvc().
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); // use the session app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
7. To change session values, simply do the following anywhere in your controller methods
session.SessionVariables.IsAuthenticated = true; session.SessionVariables.UserID = "admin"; session.Save();
And remember, the session variable class can have as many properties as you so desire. Things like browser, remote ip address, remote port, access level, etc.
8. Have Fun
*** The class file is located in the Models folder of the project. The file name is PFSXSession.cs Rename it and change the namespace, etc.
Best Regards,
Paul Sirpenski.