SlideShare uma empresa Scribd logo
1 de 7
Dot Net Training
Authorization In Asp.Net
Part IV
Dot Net Training
Today we will be discussing resource based authorization.
Often authorization is based upon the accessed resource .
E.g. a document might have an author property. So only the author would be allowed
to update it; the resource must be loaded from the document repository before an
authorization access can be made.
This can't be done with an Authorize attribute, as feature evaluation takes place before
data binding and before the code to load a resource runs inside an action.
Instead of the declarative authorization, the attribute method, we must use impervious
authorization, where a developer calls upon an authorize function within his own code.
Authorizing within a code
Authorization is executed as a service, IAuthorizationService, registered in the service
collection and available via dependency injection for Controllers to approach.
Dot Net Training
public class DocumentController : Controller
{
IAuthorizationService _authorizationService;
public DocumentController(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
}
IAuthorizationService has 2 methods: in one, you pass the resource and the policy
name and in the other, you pass the resource and a list of requirements to assess.
Task<bool> AuthorizeAsync(ClaimsPrincipal user,
object resource,
IEnumerable<IAuthorizationRequirement> requirements);
Task<bool> AuthorizeAsync(ClaimsPrincipal user,
object resource,
string policyName);
Dot Net Training
Writing a resource based handler
Now to write a handler for resource based authorization is not very much different to
write a plain requirements handler. You create a requirement, and then execute a
handler for the requirement, then specify the requirement as before and also the
resource type.
Therefore, a handler which might accept a Document resource would look as below:
public class DocumentAuthorizationHandler : AuthorizationHandler<MyRequirement,
Document>
{
public override Task HandleRequirementAsync(AuthorizationHandlerContext
context,
MyRequirement requirement,
Document resource)
{
// Ratify the requirement against the resource and identity.
return Task.CompletedTask;
}
}
Dot Net Training
public static class Operations
{
public static OperationAuthorizationRequirement Create =
new OperationAuthorizationRequirement { Name = "Create" };
public static OperationAuthorizationRequirement Read =
new OperationAuthorizationRequirement { Name = "Read" };
public static OperationAuthorizationRequirement Update =
new OperationAuthorizationRequirement { Name = "Update" };
public static OperationAuthorizationRequirement Delete =
new OperationAuthorizationRequirement { Name = "Delete" };
}
Your handler could then be executed as follows, using a hypothetical Document class
as the resource;
Dot Net Training
You can view the handler works upon OperationAuthorizationRequirement. The code
inside the handler must take the Name property of the supplied need into account
when making its evaluations.
To call an operational resource handler you are required to specify the operation when
calling AuthorizeAsync in your action. For example
if (await authorizationService.AuthorizeAsync(User, document, Operations.Read))
{
return View(document);
}
else
{
return new ChallengeResult();
}
This example checks if the user is able to perform the read function for the current
document. If authorization succeeds the view for the document will be returned.
If you want to learn ASP.Net and perfect yourself in .NET training, our CRB Tech
Solutions would be of great support for you. Join us with our updated program in
ASP.Net course.
Stay connected to CRB Tech reviews for more technical optimization and other
resources
Thank You..!
Dot Net Training

Mais conteúdo relacionado

Destaque

3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
Tongsamut vorasan
 

Destaque (8)

Task d my future vacations
Task d my future vacationsTask d my future vacations
Task d my future vacations
 
Stratigies for SME's Growth
Stratigies for SME's GrowthStratigies for SME's Growth
Stratigies for SME's Growth
 
Scriptaculous
ScriptaculousScriptaculous
Scriptaculous
 
Прибавление к инфинитиву окончания -ing
Прибавление к инфинитиву окончания -ingПрибавление к инфинитиву окончания -ing
Прибавление к инфинитиву окончания -ing
 
Focus group
Focus groupFocus group
Focus group
 
Mukesh Ambani awarded Othmer Gold Medal for Entrepreneurial Leadership
Mukesh Ambani awarded Othmer Gold Medal for Entrepreneurial LeadershipMukesh Ambani awarded Othmer Gold Medal for Entrepreneurial Leadership
Mukesh Ambani awarded Othmer Gold Medal for Entrepreneurial Leadership
 
Familia orchidáceas (orchidaceae)
Familia orchidáceas (orchidaceae)Familia orchidáceas (orchidaceae)
Familia orchidáceas (orchidaceae)
 
3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
3 37คัณฐีพระธัมมปทัฏฐกถา+ยกศัพท์แปล+ภาค๗
 

Mais de sonia merchant

Mais de sonia merchant (20)

What does dot net hold for 2016?
What does dot net hold for 2016?What does dot net hold for 2016?
What does dot net hold for 2016?
 
What does .net hold for 2016?
What does .net hold for 2016?What does .net hold for 2016?
What does .net hold for 2016?
 
Data protection api's in asp dot net
Data protection api's in asp dot netData protection api's in asp dot net
Data protection api's in asp dot net
 
Authorization p iv
Authorization p ivAuthorization p iv
Authorization p iv
 
Authorization iii
Authorization iiiAuthorization iii
Authorization iii
 
Authorization in asp dot net part 2
Authorization in asp dot net part 2Authorization in asp dot net part 2
Authorization in asp dot net part 2
 
Asp dot-net core problems and fixes
Asp dot-net core problems and fixes Asp dot-net core problems and fixes
Asp dot-net core problems and fixes
 
Search page-with-elasticsearch-and-dot-net
Search page-with-elasticsearch-and-dot-netSearch page-with-elasticsearch-and-dot-net
Search page-with-elasticsearch-and-dot-net
 
Build a-search-page-with-elastic search-and-dot-net
Build a-search-page-with-elastic search-and-dot-netBuild a-search-page-with-elastic search-and-dot-net
Build a-search-page-with-elastic search-and-dot-net
 
How to optimize asp dot-net application
How to optimize asp dot-net applicationHow to optimize asp dot-net application
How to optimize asp dot-net application
 
How to optimize asp dot net application ?
How to optimize asp dot net application ?How to optimize asp dot net application ?
How to optimize asp dot net application ?
 
10 things to remember
10 things to remember10 things to remember
10 things to remember
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
Owin and-katana-overview
Owin and-katana-overviewOwin and-katana-overview
Owin and-katana-overview
 
Top 15-asp-dot-net-interview-questions-and-answers
Top 15-asp-dot-net-interview-questions-and-answersTop 15-asp-dot-net-interview-questions-and-answers
Top 15-asp-dot-net-interview-questions-and-answers
 
Next generation asp.net v next
Next generation asp.net v nextNext generation asp.net v next
Next generation asp.net v next
 
Dot net universal apps
Dot net universal appsDot net universal apps
Dot net universal apps
 
Browser frame building with c# and vb dot net
Browser frame building  with c# and vb dot netBrowser frame building  with c# and vb dot net
Browser frame building with c# and vb dot net
 
A simplest-way-to-reconstruct-.net-framework
A simplest-way-to-reconstruct-.net-frameworkA simplest-way-to-reconstruct-.net-framework
A simplest-way-to-reconstruct-.net-framework
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Authorization in asp dot-net (part-4)

  • 2. Dot Net Training Today we will be discussing resource based authorization. Often authorization is based upon the accessed resource . E.g. a document might have an author property. So only the author would be allowed to update it; the resource must be loaded from the document repository before an authorization access can be made. This can't be done with an Authorize attribute, as feature evaluation takes place before data binding and before the code to load a resource runs inside an action. Instead of the declarative authorization, the attribute method, we must use impervious authorization, where a developer calls upon an authorize function within his own code. Authorizing within a code Authorization is executed as a service, IAuthorizationService, registered in the service collection and available via dependency injection for Controllers to approach.
  • 3. Dot Net Training public class DocumentController : Controller { IAuthorizationService _authorizationService; public DocumentController(IAuthorizationService authorizationService) { _authorizationService = authorizationService; } } IAuthorizationService has 2 methods: in one, you pass the resource and the policy name and in the other, you pass the resource and a list of requirements to assess. Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements); Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName);
  • 4. Dot Net Training Writing a resource based handler Now to write a handler for resource based authorization is not very much different to write a plain requirements handler. You create a requirement, and then execute a handler for the requirement, then specify the requirement as before and also the resource type. Therefore, a handler which might accept a Document resource would look as below: public class DocumentAuthorizationHandler : AuthorizationHandler<MyRequirement, Document> { public override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement, Document resource) { // Ratify the requirement against the resource and identity. return Task.CompletedTask; } }
  • 5. Dot Net Training public static class Operations { public static OperationAuthorizationRequirement Create = new OperationAuthorizationRequirement { Name = "Create" }; public static OperationAuthorizationRequirement Read = new OperationAuthorizationRequirement { Name = "Read" }; public static OperationAuthorizationRequirement Update = new OperationAuthorizationRequirement { Name = "Update" }; public static OperationAuthorizationRequirement Delete = new OperationAuthorizationRequirement { Name = "Delete" }; } Your handler could then be executed as follows, using a hypothetical Document class as the resource;
  • 6. Dot Net Training You can view the handler works upon OperationAuthorizationRequirement. The code inside the handler must take the Name property of the supplied need into account when making its evaluations. To call an operational resource handler you are required to specify the operation when calling AuthorizeAsync in your action. For example if (await authorizationService.AuthorizeAsync(User, document, Operations.Read)) { return View(document); } else { return new ChallengeResult(); } This example checks if the user is able to perform the read function for the current document. If authorization succeeds the view for the document will be returned. If you want to learn ASP.Net and perfect yourself in .NET training, our CRB Tech Solutions would be of great support for you. Join us with our updated program in ASP.Net course. Stay connected to CRB Tech reviews for more technical optimization and other resources