C# Identity Authorize attribute region

316 Views Asked by At

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

1

There are 1 best solutions below

0
James L On

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