HockeyJ HockeyJ - 1 year ago 173
C# Question

The AuthorizationPolicy named: 'Admin' was not found

I am learning Authentication/Authorization in .NET Core MVC.

I am trying to create a controller that can only be accessed by 'Admin', but get the following error.

An unhandled exception occurred while processing the request.

InvalidOperationException: The AuthorizationPolicy named: 'Admin' was
not found.

Here's my code. What should I do?

public async Task<string> MyMethod()
return await Task<string>.Run(() => "Hello Admin");

Answer Source

you can define the matching policy in Startup.cs

services.AddAuthorization(options =>

            authBuilder =>


the authBuilder has other methods on it, you can require claims or specific user names or custom rules using policy based authorization and control the rules from a central place in Startup

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download