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
Resource 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
after that you have to tell the application to add authentication and authorization
app.UseAuthentication();app.UseAuthorization();
And Then Create a Handler For Source
publicclassImageFileDTOHandler:AuthorizationHandler<OperationAuthorizationRequirement,ImageFileDTO>{protectedoverrideTaskHandleRequirementAsync(AuthorizationHandlerContextcontext,OperationAuthorizationRequirementrequirement,ImageFileDTOresource){//This is checking That this incoming requirment is meeting with read access or notif(requirement.Name==Operations.Read.Name){context.Succeed(requirement);}returnTask.CompletedTask;}}publicstaticclassOperations{publicstaticOperationAuthorizationRequirementCreate=newOperationAuthorizationRequirement{Name=nameof(Create)};publicstaticOperationAuthorizationRequirementRead=newOperationAuthorizationRequirement{Name=nameof(Read)};publicstaticOperationAuthorizationRequirementUpdate=newOperationAuthorizationRequirement{Name=nameof(Update)};publicstaticOperationAuthorizationRequirementDelete=newOperationAuthorizationRequirement{Name=nameof(Delete)};}
now you have to call This AuthorizeAsync Method With resource /Type and i have given a requirment Operations.Read
ImageFileDTOimageFile=newImageFileDTO();imageFile.Name="admin";//It Will Automaticlly Call the ImageFileHandler because of ImageFileDTO Type we have passed if((await_authorizationService.AuthorizeAsync(User,imageFile,Operations.Read)).Succeeded){varname=HttpContext.User.FindFirst(c =>c.Type==ClaimTypes.Name).Value;varsalary=HttpContext.User.FindFirst(c =>c.Type=="Salary").Value;ViewBag.Name=name;ViewBag.Salary=salary;returnView();}
You Can apply any custom rules or Db Related rules which defines your Authorization For Particular user in Handler
Thanks
Visit To The Repository Where Role Based Authorization Has been implemented