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

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Policy-based role checks

Policy-based role checks - ASP.NET Core Tutorial

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

Start my 1-month free trial

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. And then as a configuration, let us write in here policy that goes to…

Contents