- 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.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
builder.Services.AddAuthorization(options =>
{
//Claim is required for this policy is UserId if any user have Claim Named UserId and Value 1,2,3,4,5 then this will work otherwise this will not give you the access
options.AddPolicy("SuperUser", policy => policy.RequireClaim("UserId", "1", "2", "3", "4", "5"));
});
builder.Services.AddSingleton<IAuthorizationHandler, SalaryHandler>();
app.UseAuthentication();
app.UseAuthorization();
if (username == "admin" && password == "dotnet")
{
//if i change the UserId to 6 it cann't access Privacy method Because we have registerd A Policy With a claim named UserId in Program/startup.cs
var identity = new ClaimsIdentity(new[] { new Claim("UserId", "1") }, CookieAuthenticationDefaults.AuthenticationScheme);
var principle = new ClaimsPrincipal(identity);
var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principle);
return RedirectToAction("Index", "Home");
}
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 = "SuperUser")]
[Authorize(Policy = "SuperUser")]
public IActionResult Privacy()
{
return View();
}
- Now This Method Will only accessible to The User Whose Have Policy With Requirment Matchs That It
- Visit To The Repository Where Role Based Authorization Has been implemented
- ClaimBasedAuthorization