Question

so I have a question about the [Authorize] tag. I am required a user to have the role of admin to visit a certain view. It works great and only allows an "admin" to go to it instead of the "user". But whenever the user visits it gives me this:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Login.cshtml

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009 

So my question is, when a user visits how can I redirect them to another view?

So this is my controller for index:

[Authorize(Users = "Admin")]
        public ActionResult Index()
        {
            var user = db.User.Include(u => u.UserRole);
            return View(user.ToList());
        }
Was it helpful?

Solution

You can solve this in two ways:

  1. Let the index action be accessible by anonymous user and based on the role call different functions.
  2. Create your own custom Authorization attribute (you can find an example here).

Both the approaches are fine, with no distinct advantage/disadvantage over one and another, so you could choose any of them.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top