Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different default pages/controllers for every role type - MVC 5

So my Q is as following (MVC 5): I got several types of default pages i'd like to start when my app is getting up.

1- for anonymous user

2...5 - for the other user roles which are automatically logged (cookie).

So that's pretty much a simple "if", i know, but meanwhile i was using the User.IsInRole("RoleName") method which cannot be used in RouteConfig.cs.

If it's a simple add of a Using statement, i apologize for the simplicity of the question in advance.

Update: I've managed to do it via some:

if(User.UserInRole("RoleName") return RedirectToAction("ActionName") in my default ActionResault.

Although it's not an elegant solution, nor it defines numerous default pages, but it makes the job done.

like image 227
Maratto Avatar asked Dec 18 '13 13:12

Maratto


2 Answers

It's not as simple as you're thinking it is.

There are several scenarios you have to think about when doing this kind of thing. For example...

Scenario A:

  1. Anonymous user comes to your website homepage.
  2. User clicks "Login".
  3. User Logs in.
  4. Where does the system direct the user?

Scenario B:

  1. Anonymous user accesses your website via some deep link other than the homepage.
  2. User clicks "Login".
  3. User Logs in.
  4. Where does the system direct the user?

Scenario C:

  1. User visits some deep link other than the homepage that has previously authenticated to the website and still has a valid authentication cookie.
  2. Where does the website direct the user?

I'm in a bit of a hurry, so it's probably hard to understand what I'm talking about. The point is, you have to think about the different ways a user might access your site. Typically, I wire this kind of functionality into the registration and login process. You won't be able to do what you want with routing.

like image 159
Alex Dresko Avatar answered Sep 28 '22 05:09

Alex Dresko


One way to resolve this is to create a "Base" controller from which all your other controllers inherit. In this controller you could write your IF/CASE statements and you could override the base controller's Initialize, OnActionExecuting and OnResultExecuting methods to redirect where you want your user to end up based on your logic.

So basically you would create a base controller like this:

public class BaseController : Controller
{
   protected string RedirectPath = string.Empty;
   protected bool DoRedirect = false;

   protected override void Initialize(RequestContext requestContext)
   {
      base.Initialize(requestContext);
      // Your logic to determine whether to redirect or not goes here. Bellow is an example...
      if (requestContext.HttpContext.User.IsInRole("RoleName"))
      {
         DoRedirect = true;
         RedirectPath = Url.Action("SomeAction", "SomeController");
      }
   }

   protected override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       base.OnActionExecuting(filterContext);
       if (DoRedirect)
       {
          // Option 1: TRANSFER the request to another url
          filterContext.Result = new TransferResult(RedirectPath);
          // Option 2: REDIRECT the request to another url
          filterContext.Result = new RedirectResult(RedirectPath);
       }
   }
}

And then each one of your controller which has to adhere to this user role redirect logic needs to inherit from the BaseController:

public class HomeController : BaseController
{
    // Controller logic here...
}
like image 30
Marko Avatar answered Sep 28 '22 05:09

Marko