c# - The AuthorizationPolicy named: 'Admin' was not found -


i learning authentication/authorization in .net core mvc.

i trying create controller can accessed 'admin', following error.

an unhandled exception occurred while processing request.

invalidoperationexception: authorizationpolicy named: 'admin' not found.

here's code. should do?

[httpget("~/test")] [authorize("admin")] public async task<string> mymethod() {     return await task<string>.run(() => "hello admin"); } 

you can define matching policy in startup.cs

services.addauthorization(options =>   {          options.addpolicy("admin",             authbuilder =>             {                 authbuilder.requirerole("administrators");             });   }); 

the authbuilder has other methods on it, can require claims or specific user names or custom rules using policy based authorization , control rules central place in startup https://docs.asp.net/en/latest/security/authorization/policies.html


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -