0

Good evening I have an .NET Core MVC C# application that involves data stored for a child (ApplicationUser and a User Table - Role= "Child"). Parents/Guardians can create a profile (ApplicationUser and Parent Table). They can only see their children data.

What's the best way to "store" the current child ID they are viewing until they "switch" some way to another child ? Do you guys use cookies ? TempData? Viewbags ? Maybe a clean example I can look at ?

What's your strategy to store their first Child ID and take them to a default View for that child?

Thank you !

1 Answers1

2

You can store child ID list in HttpContext.Session.

////  Child IDs 
List<string> Ids= new List<string>();

//// To set value in session
HttpContext.Session.Set<List<string>>("child_ids", Ids);

//// To Get Value from Session
List<string> getIds = HttpContext.Session.Get<List<string>>("child_ids");

Then you can create custom policy to access their children data.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • There is no HttpContext in .Net Core MVC Core anymore. – lbrettsinclair Sep 15 '22 at 17:32
  • @lbrettsinclair This is just a sample code, you need to register `HttpContextAccessor` in startup.cs(Program.cs) file and inject IHttpContextAccessor in your controller, then you can access `HttpContext` [by using `_httpContextAccessor.HttpContext`](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-6.0#httpcontext-isnt-thread-safe). – Jason Pan Sep 16 '22 at 01:47