Add Session Extensions
This commit is contained in:
parent
d3970fba18
commit
c5f0478e08
19
Sessions/SessionExtensions.cs
Normal file
19
Sessions/SessionExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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 =>
|
||||
|
Loading…
Reference in New Issue
Block a user