From the course: ASP.NET Core Identity: Authorization Management

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Policy-based role checks

Policy-based role checks

- [Instructor] We learned in the last part that by using the authorize attribute we can define role checks. And in .NET Core, role requirements can also be expressed using a policy instead of role checks. So, let us go to our project and see how we can use policy checks instead of role checks with the authorize attribute. To create a new policy, we need to go to the Startup.cs file and then go to the ConfigureServices method. Just before the services.AddMvc, let us add our new policy configuration. So, for that we write in here services.AddAuthorization. And then inside here we write options that go to inside curly brackets, we can define our policy. So for that we write in here, options.AddPolicy. And for the AddPolicy method, we need to provide two parameters. The first one is going to be the policy name, and the second one, the policy configuration. So, let us define in here as the policy name, the TeachersOnly.…

Contents