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.

Using a func to fulfill a policy

Using a func to fulfill a policy - ASP.NET Core Tutorial

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

Start my 1-month free trial

Using a func to fulfill a policy

- [Instructor] We learned so far how to create a custom authorization requirement in .NET Core. But when building apps, there might be cases where we do not need to implement custom files for custom requirements, because it might be possible to express the requirement in simple code. And to create a fulfilling policy in code, we can use the Func delegate. So, let us go to our project and implement a policy check, which checks only if the user claims are issued by a trusted issuer. So in our project, we need to go to our Startup.cs file. In here, scroll down to the ConfigureServices method. And just after the MinimumAgeRequirement write options.AddPolicy. I will name this policy TrustedUser. And we can write policy that goes to policy.RequireAssertion. And then here we need to use the authorization handler context. So we can just write context, that goes to context.User.HasClaim. And we said that we wanted to check only if the issuer was a trusted one. So we can just write in here, c…

Contents