SlideShare a Scribd company logo
1 of 30
Download to read offline
ASP.NET MVC
MVC Core
Eng. Basma Hussien
MVC Filters
MVC Filters
• In ASP.NET MVC, a user request is routed to the appropriate controller and action
method. However, there may be circumstances where you want to execute some logic
before or after an action method executes. ASP.NET MVC provides filters for this
purpose.
• ASP.NET MVC Filter is a custom class where you can write custom logic to execute
before or after an action method executes. Filters can be applied to an action method or
controller
• Filters can be applied to an action method or controller
• You could apply filters in a declarative or programmatic way:
• Declarative means by applying a filter attribute to an action method or
controller class
• Programmatic means by implementing a corresponding interface or abstract
class
MVC Filters (Cont.)
• The ASP.NET MVC framework supports four different types of filters:
• Authorization Filters − Implements the IAuthorizationFilter attribute.
• Action Filters − Implements the IActionFilter attribute.
• Result Filters − Implements the IResultFilter attribute.
• Exception Filters − Implements the IExceptionFilter attribute.
• Filters are executed in the order listed above. For example, authorization filters are always
executed before action filters and exception filters are always executed after every other type of
filter.
• Authorization filters are used to implement authentication and authorization for controller
actions.
MVC Filters Types
MVC Filters Types
• MVC provides different types of filters. The following table list filter types, built-in
filters, and interface that must be implemented to create custom filters.
Custom Filters
• To create your own custom filter, ASP.NET MVC framework provides a
base class which is known as ActionFilterAttribute. This class
implements both IActionFilter and IResultFilter interfaces and both are
derived from the Filter class.
• Action selector is the attribute that can be applied to the action methods. It
helps the routing engine to select the correct action method to handle a
particular request. MVC 5 includes the following action selector attributes:
• ActionName
• NonAction
• ActionVerbs (AcceptVerbs)
Action Selectors
[ActionName("Find")]
public ActionResult GetById(int id)
{
return View();
}
Action Selectors (Cont.)
[NonAction]
public Student GetStudent(int id)
{
return studentList.Where(s => s.StudentId ==
id).FirstOrDefault();
}
Conventional routing
& Attribute Routing
• In ASP.NET Core, routing is handled by routing middleware, which matches the URLs of
incoming requests to actions or other endpoints.
• Controller actions are either conventionally routed or attribute-routed.
• Conventional routing is similar to the route table approach used in ASP.NET MVC and Web
API. Whether you're using conventional, attribute, or both, you need to configure your app to
use the routing middleware.
• To use the middleware, add the following code to your Program.cs file:
• app.UseRouting();
Routing
Conventional Routing
• With conventional routing, you set up one or more conventions that will be used to match incoming
URLs to endpoints in the app.
• In ASP.NET Core, these endpoints may be controller actions, like in ASP.NET MVC or Web API.
• In addition to (UseRouting), this configuration specifies a default routing convention, which is the fairly
standard {controller}/{action}/{id?} pattern that's been recommended since the first versions of
ASP.NET MVC:
• app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Conventional Routing
Attribute Routing
• Apply attribute routing on any action:
• //...../Order/GetOrders/3
• //...../customers/3/Orders
[Route("customers/{customerId}/orders")]
public ActionResult GetOrders(int customerId) {…}
Attribute Routing
• RoutePrefix on a controller:
[Route(“Company/Finance")]
• Then on each action in controller you should specify a specific route:
[Route("customers/{id}")]
[Route("")]
RoutePrefix
Use a tilde (~) on the method attribute to override the route prefix:
[Route("mvc/books")]
public class BooksController : Controller
{
// GET /mvc/authors/1/books
[Route("~/mvc/authors/{authorId:int}/books")]
public IEnumerable<Book> GetByAuthor(int authorId) { ... }
}
Override RoutePrefix
• To apply Route Constraints:
[Route("users/{id:int:min(10)}")]
public User GetUserById(int id) { ... }
Route Constraints
Constraint Description Example
Alpha Matches uppercase or lowercase Latin alphabet characters (a-z,
A-Z)
{x:alpha}
Bool Matches a Boolean value. {x:bool}
Datetime Matches a DateTime value. {x:datetime}
Decimal Matches a decimal value. {x:decimal}
Double Matches a 64-bit floating-point value. {x:double}
Float Matches a 32-bit floating-point value. {x:float}
Guid Matches a GUID value. {x:guid}
Int Matches a 32-bit integer value. {x:int}
Length Matches a string with the specified length or
within a specified range of lengths.
{x:length(6)}
{x:length(1,20)}
Long Matches a 64-bit integer value. {x:long}
Max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
Min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
Range Matches an integer within a range of values. {x:range(10,50)}
Regex Matches a regular expression. {x:regex(^d{3}-d{3}-
d{4}$)}
Membership & Identity Authentication
ASP.NET Membership
• It was designed to solve site membership requirements that were
common in 2005, which involved Forms Authentication, and a
SQL Server database for user names, passwords, and profile data.
• Today there is a much broader array of data storage options for
web applications, and most developers want to enable their sites to
use social identity providers for authentication and authorization
functionality.
ASP.NET Membership Limitations
• The database schema was designed for SQL Server and you can't
change it.
• Membership database is limited to describe users identity.
• Since the log-in/log-out functionality is based on Forms
Authentication, the membership system can't use OWIN.
ASP.NET Identity
ASP.NET Identity was developed with the following goals:
• One ASP.NET Identity system
• Persistence control
• Claims Based
• Role provider
• Social Login Providers
• OWIN Integration
• Unit testability
Validate MVC App
Using External Logins
Live Accounts Authentication
• You should Choose “Individual User” Authentication MVC template
when creating your web application
• You have to create a project on “Google.developer.console” or what so
ever provider you will use for authentication process delegation
• You need a “Client ID & Client Secret” to be able to use provider
API for authenticating your web application
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf

More Related Content

Similar to ASP.NET MVC_Routing_Authentication_Aurhorization.pdf

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8Thomas Robbins
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaJignesh Aakoliya
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVCSagar Kamate
 
(ATS6-DEV03) Building an Enterprise Web Solution with AEP
(ATS6-DEV03) Building an Enterprise Web Solution with AEP(ATS6-DEV03) Building an Enterprise Web Solution with AEP
(ATS6-DEV03) Building an Enterprise Web Solution with AEPBIOVIA
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantNitin Sawant
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introductionFajar Baskoro
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxAbhijayKulshrestha1
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3Ilio Catallo
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)Amit Ranjan
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...WebStackAcademy
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introductionBhagath Gopinath
 
Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)LearningTech
 

Similar to ASP.NET MVC_Routing_Authentication_Aurhorization.pdf (20)

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 
MVC 4
MVC 4MVC 4
MVC 4
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVC
 
(ATS6-DEV03) Building an Enterprise Web Solution with AEP
(ATS6-DEV03) Building an Enterprise Web Solution with AEP(ATS6-DEV03) Building an Enterprise Web Solution with AEP
(ATS6-DEV03) Building an Enterprise Web Solution with AEP
 
Asp.net,mvc
Asp.net,mvcAsp.net,mvc
Asp.net,mvc
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introduction
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptx
 
Mvc
MvcMvc
Mvc
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 

Recently uploaded

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Recently uploaded (20)

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

ASP.NET MVC_Routing_Authentication_Aurhorization.pdf

  • 3. MVC Filters • In ASP.NET MVC, a user request is routed to the appropriate controller and action method. However, there may be circumstances where you want to execute some logic before or after an action method executes. ASP.NET MVC provides filters for this purpose. • ASP.NET MVC Filter is a custom class where you can write custom logic to execute before or after an action method executes. Filters can be applied to an action method or controller
  • 4. • Filters can be applied to an action method or controller • You could apply filters in a declarative or programmatic way: • Declarative means by applying a filter attribute to an action method or controller class • Programmatic means by implementing a corresponding interface or abstract class MVC Filters (Cont.)
  • 5. • The ASP.NET MVC framework supports four different types of filters: • Authorization Filters − Implements the IAuthorizationFilter attribute. • Action Filters − Implements the IActionFilter attribute. • Result Filters − Implements the IResultFilter attribute. • Exception Filters − Implements the IExceptionFilter attribute. • Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. • Authorization filters are used to implement authentication and authorization for controller actions. MVC Filters Types
  • 6. MVC Filters Types • MVC provides different types of filters. The following table list filter types, built-in filters, and interface that must be implemented to create custom filters.
  • 7. Custom Filters • To create your own custom filter, ASP.NET MVC framework provides a base class which is known as ActionFilterAttribute. This class implements both IActionFilter and IResultFilter interfaces and both are derived from the Filter class.
  • 8.
  • 9.
  • 10. • Action selector is the attribute that can be applied to the action methods. It helps the routing engine to select the correct action method to handle a particular request. MVC 5 includes the following action selector attributes: • ActionName • NonAction • ActionVerbs (AcceptVerbs) Action Selectors
  • 11. [ActionName("Find")] public ActionResult GetById(int id) { return View(); } Action Selectors (Cont.) [NonAction] public Student GetStudent(int id) { return studentList.Where(s => s.StudentId == id).FirstOrDefault(); }
  • 13. • In ASP.NET Core, routing is handled by routing middleware, which matches the URLs of incoming requests to actions or other endpoints. • Controller actions are either conventionally routed or attribute-routed. • Conventional routing is similar to the route table approach used in ASP.NET MVC and Web API. Whether you're using conventional, attribute, or both, you need to configure your app to use the routing middleware. • To use the middleware, add the following code to your Program.cs file: • app.UseRouting(); Routing
  • 15. • With conventional routing, you set up one or more conventions that will be used to match incoming URLs to endpoints in the app. • In ASP.NET Core, these endpoints may be controller actions, like in ASP.NET MVC or Web API. • In addition to (UseRouting), this configuration specifies a default routing convention, which is the fairly standard {controller}/{action}/{id?} pattern that's been recommended since the first versions of ASP.NET MVC: • app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Conventional Routing
  • 17. • Apply attribute routing on any action: • //...../Order/GetOrders/3 • //...../customers/3/Orders [Route("customers/{customerId}/orders")] public ActionResult GetOrders(int customerId) {…} Attribute Routing
  • 18. • RoutePrefix on a controller: [Route(“Company/Finance")] • Then on each action in controller you should specify a specific route: [Route("customers/{id}")] [Route("")] RoutePrefix
  • 19. Use a tilde (~) on the method attribute to override the route prefix: [Route("mvc/books")] public class BooksController : Controller { // GET /mvc/authors/1/books [Route("~/mvc/authors/{authorId:int}/books")] public IEnumerable<Book> GetByAuthor(int authorId) { ... } } Override RoutePrefix
  • 20. • To apply Route Constraints: [Route("users/{id:int:min(10)}")] public User GetUserById(int id) { ... } Route Constraints
  • 21. Constraint Description Example Alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) {x:alpha} Bool Matches a Boolean value. {x:bool} Datetime Matches a DateTime value. {x:datetime} Decimal Matches a decimal value. {x:decimal} Double Matches a 64-bit floating-point value. {x:double} Float Matches a 32-bit floating-point value. {x:float} Guid Matches a GUID value. {x:guid} Int Matches a 32-bit integer value. {x:int}
  • 22. Length Matches a string with the specified length or within a specified range of lengths. {x:length(6)} {x:length(1,20)} Long Matches a 64-bit integer value. {x:long} Max Matches an integer with a maximum value. {x:max(10)} maxlength Matches a string with a maximum length. {x:maxlength(10)} Min Matches an integer with a minimum value. {x:min(10)} minlength Matches a string with a minimum length. {x:minlength(10)} Range Matches an integer within a range of values. {x:range(10,50)} Regex Matches a regular expression. {x:regex(^d{3}-d{3}- d{4}$)}
  • 23. Membership & Identity Authentication
  • 24. ASP.NET Membership • It was designed to solve site membership requirements that were common in 2005, which involved Forms Authentication, and a SQL Server database for user names, passwords, and profile data. • Today there is a much broader array of data storage options for web applications, and most developers want to enable their sites to use social identity providers for authentication and authorization functionality.
  • 25. ASP.NET Membership Limitations • The database schema was designed for SQL Server and you can't change it. • Membership database is limited to describe users identity. • Since the log-in/log-out functionality is based on Forms Authentication, the membership system can't use OWIN.
  • 26. ASP.NET Identity ASP.NET Identity was developed with the following goals: • One ASP.NET Identity system • Persistence control • Claims Based • Role provider • Social Login Providers • OWIN Integration • Unit testability
  • 27.
  • 28. Validate MVC App Using External Logins
  • 29. Live Accounts Authentication • You should Choose “Individual User” Authentication MVC template when creating your web application • You have to create a project on “Google.developer.console” or what so ever provider you will use for authentication process delegation • You need a “Client ID & Client Secret” to be able to use provider API for authenticating your web application