How to do it...

The authorization filter goal is to restrict action methods individually, or by controller to specific users, roles, or claims. It always runs before the action is executed:

  1. A classic way to use the Authorization filter is to add this filter at the controller level, and override with the AllowAnonymous attribute at Action level, as shown in the following code:
[Authorize]public class AccountController : Controller{  [HttpGet]  [AllowAnonymous]  public IActionResult Login(string returnUrl = null)  {    ViewData["ReturnUrl"] = returnUrl;    return View();  }  [HttpGet]  [AllowAnonymous]  public IActionResult Register(string returnUrl = null)  {    ViewData["ReturnUrl"] = returnUrl;    return View();  }  [HttpPost]  [ValidateAntiForgeryToken] public ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.