Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put code to Redirect user back to page until predicate is true?

I wish to replicate similar functionality that ASP provides, that when a user is not authenticated, they are redirected to a specific page (the login page).

However, Instead of the login page, I wish to persistently send the user to a specific action on a controller until the user fills in a form to change their password for the first time when they ARE authenticated.

When they first log in, I am checking the following and redirecting them here accordingly:

if (user.HasChangedPassword())
{
    // This user hasn't changed their password to something more secure, send them to the page to change it
    return this.Redirect("Manage");
}

I would like to take this code out of my login code and have it checked site wide, so that the user is restricted to the single "Manage" action on my controller until they have set a secure password.

Please can you tell me where I could put this code so that it is effective site wide within my project?

The only thing I can think of is to check this in each of my controllers which seems very untidy.

like image 415
Luke Avatar asked Oct 20 '25 18:10

Luke


1 Answers

Create an action filter and register it as a global filter.

ASP.NET MVC Action filters

Example of registering filter globally

like image 123
Jace Rhea Avatar answered Oct 22 '25 09:10

Jace Rhea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!