SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a 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_Hour
Dilip Patel
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVC
Sagar Kamate
 
Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)
LearningTech
 

Similar a 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
 

Último

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Marc Lester
 

Último (20)

Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 

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