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(new SalaryRequirement(30000)));});
after that you have to tell the application to add authentication and authorization
app.UseAuthentication();
app.UseAuthorization();
After that you have to create a Requirment For Claim
using Microsoft.AspNetCore.Authorization;namespace PolicyBasedAuthorization.Service;publicclassSalaryRequirement:IAuthorizationRequirement{publicSalaryRequirement(intsalary)=>Salary=salary;publicintSalary{get;}}
And Then Create a Handler For That requirment and you can also handle multiple requirments in single handler
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")]