0

Today, I was owndering if I could use the [Authorize] attribute with regions in C#

I never found a satisfying answer, but I think I found an answer

[Authorize(Roles = "Admin")]
#region testRegion

// GET: /Home/Index
public virtual ActionResult Index()
{
    return View();
}

#endregion

With this code, only users with the "Admin" role can see this page

  • The question doesn't make sense. Regions are just editor tools, nothing else. If you want to restrict access to a page, just put the attribute on the controller or specific action method.. – Jeroen Vannevel May 20 '17 at 11:00

1 Answers1

1

No, you can't.

Regions are only for human readability and the're ignored by the compiler.

In your example, the attribute is being applied to the Index() method

James L
  • 16,456
  • 10
  • 53
  • 70