I'm a beginning .Net developer and working in MVC on a basic rental solution.
When a user opens the website he comes to the HomeController/Login page. Here he has to enter his last name and postal code. The code then checks whether or not the combo is in a customers table.
If it is the customer object is stored in a httpcontext.session variable "loggedinuser". The website then redirect to a rental controller/overview action.
In the rental controller I want to apply an actionfilter which checks whether "loggedinuser" is null or empty. if yes the website has to redirect to Home/Logins again.
I've been looking on internet and found some sollutions with usage of:
var ctx = filtercontext.HttpContext;
if(ctx.Session["loggedinuser"] !=null)
However if I do that I get an error saying that indexing [ ] is not possible wit ISession.
Can you help me out here? How do I approach this and where do I store the actionclass?
If your project is an ASP.NET MVC project, your code is correct.
If your project is an ASP.NET Core MVC project, you need to install
Microsoft.AspNetCore.Session first, and then enable session middleware in the
Startup file.
Startup.cs
The order of middleware is important. Call UseSession after UseRouting and
before UseEndpoints.
public class TestFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var value = filterContext.HttpContext.Session.GetString("loggedinuser");
}
}
Controller
public class Test66Controller : Controller
{
public IActionResult Index()
{
HttpContext.Session.SetString("loggedinuser", "test");
return View();
}
[TestFilter]
public IActionResult Test()
{
return View();
}
}
You can click this link to learn more about Configure session state.
Here is the result.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
1 Points
1 Post
Check if a current session variable not null before actions are executed
Dec 08, 2020 04:13 PM|TinoS|LINK
Hi,
I'm a beginning .Net developer and working in MVC on a basic rental solution.
When a user opens the website he comes to the HomeController/Login page. Here he has to enter his last name and postal code. The code then checks whether or not the combo is in a customers table.
If it is the customer object is stored in a httpcontext.session variable "loggedinuser". The website then redirect to a rental controller/overview action.
In the rental controller I want to apply an actionfilter which checks whether "loggedinuser" is null or empty. if yes the website has to redirect to Home/Logins again.
I've been looking on internet and found some sollutions with usage of:
var ctx = filtercontext.HttpContext;
if(ctx.Session["loggedinuser"] !=null)
However if I do that I get an error saying that indexing [ ] is not possible wit ISession.
Can you help me out here? How do I approach this and where do I store the actionclass?
All-Star
52091 Points
23211 Posts
Re: Check if a current session variable not null before actions are executed
Dec 08, 2020 07:29 PM|mgebhard|LINK
You have not mentioned what framework you are using. If ASP.NET not Core or .NET 5 then it's just...
Reference documentation
https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178581(v=vs.100)
I recommend using standard authentication/authorization libraries that come with MVC. The libraries handle empty user session.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/
Contributor
2360 Points
676 Posts
Re: Check if a current session variable not null before actions are executed
Dec 09, 2020 09:43 AM|YihuiSun|LINK
Hi TinoS,
public class TestFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var value = filterContext.HttpContext.Session.GetString("loggedinuser"); } }
Best Regards,
YihuiSun