PolicyBasedAuthorization
No description available
Install / Use
/learn @rajguptaH/PolicyBasedAuthorizationREADME
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.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
Now You have To Add Policy in Startup
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)));
});
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();
After that you have to create a Requirment For Claim
using Microsoft.AspNetCore.Authorization;
namespace PolicyBasedAuthorization.Service;
public class SalaryRequirement : IAuthorizationRequirement
{
public SalaryRequirement(int salary) =>
Salary = salary;
public int Salary { get; }
}
And Then Create a Handler For That requirment and you can also handle multiple requirments in single handler
public class SalaryHandler : AuthorizationHandler<SalaryRequirement>
{
protected override Task HandleRequirementAsync(
AuthorizationHandlerContext context, SalaryRequirement requirement)
{
var nameClaim = context.User.FindFirst(
c => c.Type == "Salary");
if (nameClaim != null)
{
var salary = int.Parse(nameClaim.Value);
if (salary >= requirement.Salary)
{
context.Succeed(requirement);
}
}
return Task.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")]
[Authorize(Policy = "AtLeast30K")]
public IActionResult Privacy()
{
return View();
}
- Now This Method Will only accessible to The User Whose Have Policy With Requirment Matchs That It
Thanks
- Visit To The Repository Where Role Based Authorization Has been implemented
- RoleBaseAuthorization
Related Skills
node-connect
350.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
