Add Session Extensions

This commit is contained in:
luke-else 2021-11-07 22:06:16 +00:00
parent d3970fba18
commit c5f0478e08
2 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace EFB.Sessions
{
public static class SessionExtensions
{
public static void SetObject(this ISession session, string key, object value)
{//Sets the object of a session to Object
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObject<T>(this ISession session, string key)
{//Gets a session of known type (T)
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
}

View File

@ -24,6 +24,7 @@ namespace EFB
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSession();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -44,6 +45,8 @@ namespace EFB
app.UseRouting();
app.UseSession();
app.UseAuthorization();
app.UseEndpoints(endpoints =>