ASP.NET MVC: Restricting Access Using a URL

The URL of the administration section of my website always starts with Admin/. Is it possible in ASP.NET MVC to restrict access to users using this part of the URL?

Obviously, I would save it [Authorize(Roles = "Administrator")]on the appropriate controllers and actions, but I wonder if it will be faster for the application if it can just look at the URL instead of entering the code.

+3
source share
3 answers

Found the answer in Stephen Sanderson's book, Pro ASP.NET MVC Framework .

Put the following code in your web.config file.

<location path ="Admin">
  <system.web>
    <authorization>
      <deny users="?"/>
      <allow roles="Administrator"/>
      <deny users="*"/>
    </authorization>
  </system.web>
</location>

, URL-, ~/Admin/*, , , "".

+2

, . Actions , (, , , ) URL, .

, Unit Test .

+1

BaseAdminController, :

[Authorize(Roles = "Administrator")]
public class BaseAdminController : Controller {
}

, URL-, , , , , . , Admin .

+1
source

Source: https://habr.com/ru/post/1716732/


All Articles