How to add global `AuthorizeFilter` or `AuthorizeAttribute` in ASP.NET Core?

后端 未结 3 2003
情歌与酒
情歌与酒 2020-12-03 07:01

In ASP.NET MVC 4 and below we just add the following in Global.asax:

GlobalFilters.Filters.Add(new AuthorizeAttribute() { Roles = \"Admin, S         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 07:26

    In case if you are using the Razor Page flavor of the ASP.NET Core 2.0 you could add global filters as follows:

    services.AddMvc()
    .AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizeFolder("/"); // Require users to be authenticated.
                options.Conventions.AuthorizeFolder("/", "YourPolicyName"); // Require a policy to be full filled globally.
            });
    

提交回复
热议问题