Skip to content

in this i have implemented claim based authorization in asp dot net core mvc

License

Notifications You must be signed in to change notification settings

rajguptaH/ClaimBasedAuthorization

Repository files navigation

Claim 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.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

Now You have To Add Policy in Startup

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"));
});

Then you have add Singleton

builder.Services.AddSingleton<IAuthorizationHandler, SalaryHandler>();

after that you have to tell the application to add authentication and authorization

app.UseAuthentication();
app.UseAuthorization();

And Then You Have to authenticate user like this

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

Thanks

About

in this i have implemented claim based authorization in asp dot net core mvc

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published