You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Policy Based Authorization in asp dot net core Mvc
You Have To Tell In The Startup You Are going to use authentication write below code in start.cs and this says you are adding the authentication service in your application using a cookies
builder.Services.AddAuthorization(options =>{//"AtLeast30K" is policy name and the SalaryRequirement Is Requirment for this Policy Parameter options.AddPolicy("AtLeast30K", policy =>policy.Requirements.Add(newSalaryRequirement(30000)));});
And Then Create a Handler For That requirment and you can also handle multiple requirments in single handler
publicclassSalaryHandler:AuthorizationHandler<SalaryRequirement>{protectedoverrideTaskHandleRequirementAsync(AuthorizationHandlerContextcontext,SalaryRequirementrequirement){varnameClaim=context.User.FindFirst(
c =>c.Type=="Salary");if(nameClaim!=null){varsalary=int.Parse(nameClaim.Value);if(salary>=requirement.Salary){context.Succeed(requirement);}}returnTask.CompletedTask;}}
now you have to use Policy base authorization for a method or a controller so you can simply provide a parameter called Policy to the Authorize attribute Like this [Authorize(Policy = "AtLeast30K")]