0

Goal:
Make it easier to validate if the same user or not in relation to DRY.

Problem:
Every time when you go to the next page by using ActionResult you have to add a

if (Session["UserID"] != null)  

in every ActionResult's content in order to make a validation.

Question:
Is there another approach in order to make it easier and better than today?

My idea is to use this validation on top of the ActionResult

Propose (is it possible to do it?)

[validation]
    public ActionResult UserDashBoard()  
    {  
           return View();  
    } 

And not like this today:

    public ActionResult UserDashBoard()  
    {  
        if (Session["UserID"] != null)  
        {  
            return View();  
        } else  
        {  
            return RedirectToAction("Login");  
        }  
    } 

The source that I am using is from this page.

https://www.c-sharpcorner.com/article/simple-login-application-using-Asp-Net-mvc/

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
What'sUP
  • 12,102
  • 23
  • 71
  • 120
  • 1
    See related: [How do you create a custom AuthorizeAttribute in ASP.NET Core?](https://stackoverflow.com/questions/31464359/) – Igor Jun 06 '19 at 13:02
  • As a side note putting the authenticated state in Session is not a good idea. Ideally requests should all be stateless (so Session should not be needed at all) and authentication state should come from the request itself using either a cookie or an http header. – Igor Jun 06 '19 at 13:09
  • You could have this validation in the constructor of the controller. if you need a code example let me know. – Claudio Corchez Jun 06 '19 at 13:10
  • Hi Claudio, if possible I am interested in taking part of the this code example. – What'sUP Jun 06 '19 at 13:21
  • 1
    @What'sUP please read this: https://stackoverflow.com/questions/4793452/mvc-redirect-inside-the-constructor , it is exactly what I use in one of the CRM I have buit – Claudio Corchez Jun 06 '19 at 13:39
  • I couldn't get what "if the same user or not" validation aims? – ibubi Jun 10 '19 at 10:18

0 Answers0