SlideShare una empresa de Scribd logo
1 de 25
MVC Filters


  Eyal Vardi
  CEO E4D Solutions LTD
  Microsoft MVP Visual C#
  blog: www.eVardi.com
Agenda
           What is Filters?

           Built-in Filters

           Filter Interfaces

           Custom Filters

           Filter Providers




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
ASP.NET MVC Filters
           Filters are custom classes that provide both a
            declarative and programmatic means to add
            pre-action and post-action behavior to
            controller action methods.

                      [HandleError]
                      [Authorize]
                      public class CourseController : Controller
                      {
                         [OutputCache]
                         [RequireHttps]
                         public ActionResult Net( string name )
                         {
                             ViewBag.Course = BL.GetCourse(name);
                             return View();
                         }
                      }
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Interfaces

                                           Action Method                   Action Result




                            1                              2                               3   4




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Controller Context
                        1                                                            6




                        2                               3                        4   5




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Order
           Filters run in the following order:
                  Authorization filters

                  Action filters

                  Response filters

                  Exception filters




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Controller & Filters
           The Controller class implements each of the
            filter interfaces.
           You can implement any of the filters for a
            specific controller by overriding the
            controller's On<Filter> method.
                  OnAuthorization
                  OnActionExecuting
                  OnActionExecuted
                  OnResultExecuting
                  OnResultExecuted
                  OnException


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
“Install” Filters
           You can “Install” a filter in the following ways:
                  Attribute on Actions or Controllers

                  Add to Global Filters




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IAuthorizationFilter
           Make security decisions about whether to
            execute an action method.
                  AuthorizeAttribute

                  RequireHttpsAttribute




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Authorize Attribute Sample
           Authorization won’t be granted unless both
            conditions are met, Users & Roles.


            [Authorize(Users=“Eyal, Oz”, Roles=“Admin”)]
            public class CourseController : Controller
            {
               public ActionResult Net( string name )
               {
                   ViewBag.Course = BL.GetCourse(name);
                   return View();
               }
            }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Authorization Policy


          public class MyAuthorizationAttribute : AuthorizeAttribute
          {
             protected override bool AuthorizeCore(HttpContextBase httpContext)
             {
                  return httpContext.Request.IsLocal ||
                         AuthorizeCore(httpContext);
              }
           }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IActionFilter Interface
           OnActionExecuting
                  Runs before the action method.

           OnActionExecuted
                  Runs after the action method




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IActionFilter Context’s




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IResultFilter Interface
             OnResultExecuting
                  Runs before the ActionResult object is executed.

           OnResultExecuted
                  Runs after the result.
                  Can perform additional processing of the result.
                  The OutputCacheAttribute is one example of a result filter.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IResultFilter Context’s




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IExceptionFilter
           Execute if there is an unhandled exception
            thrown during the execution of the ASP.NET
            MVC pipeline.
                  Can be used for logging or displaying an error page.

                  HandleErrorAttribute is one example of an exception filter.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Handle Error Attribute
           You can specify an exception and the names
            of a view and layout.
           Works only when custom errors
            are enabled in the Web.config file
                  <customErrors mode="On" /> inside
                   the <system.web>


           The view get
            HandleErrorInfo




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Exception Filter
        public class MyExceptionAttribute: FilterAttribute, IExceptionFilter
        {
             public void OnException(ExceptionContext filterContext)
             {
                  if (!filterContext.ExceptionHandled &&
                            filterContext.Exception is NullReferenceException)
                  {
                           filterContext.Result =
                                new RedirectResult("/MyError.html");
                           filterContext.ExceptionHandled = true;

                        }
                }
        }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filters


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
First                    Global                    Controller                 Action           Last
   Filter I                   Filter I                    Filter I                 Filter I       Filter I
   Filter II                  Filter II                   Filter II                Filter II      Filter II


                                          Action Method                   Action Result




      Authorization                          Action                         Result              Exception
       Filter I                        Filter I     Filter I          Filter I     Filter I    Filter II
        Filter I                                                                                Filter II
           Filter I                                                                                Filter II
             Filter I                  Filter I     Filter I          Filter I     Filter I          Filter II
               Filter I                                                                                Filter II
                                       Filter I     Filter I          Filter I     Filter I
      Filter II
       Filter II                       Filter I     Filter I          Filter I                 Filter I
          Filter II                                                                Filter I     Filter I
            Filter II                                                                              Filter I
              Filter II                Filter I     Filter I          Filter I     Filter I          Filter I
                                                                                                       Filter I
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Filter
           You can create a filter in the following ways:
                  Override one or more of the controller's On<Filter>
                   methods.

                  Create an attribute class that derives from
                   ActionFilterAttribute or FilterAttribute.
                  Register a filter with the filter provider
                   (the FilterProviders class).

                  Register a global filter using the GlobalFilterCollection class.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Attribute Filters




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Global Filters




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Providers
           By default, ASP.NET MVC registers the
            following filter providers:
                  Filters for global filters.

                  FilterAttributeFilterProvider for filter attributes.

                  ControllerInstanceFilterProvider for controller instances.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
The Filter Provider Interface
           The GetFilters method returns all of the
            IFilterProvider instances in the service
            locator.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

Más contenido relacionado

La actualidad más candente (20)

Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Introduction to ASP.NET Core
Introduction to ASP.NET CoreIntroduction to ASP.NET Core
Introduction to ASP.NET Core
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Php
PhpPhp
Php
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
 
PHP MVC
PHP MVCPHP MVC
PHP MVC
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Architecture java j2 ee a partager
Architecture java j2 ee a partagerArchitecture java j2 ee a partager
Architecture java j2 ee a partager
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training session
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
OAuth2 - Introduction
OAuth2 - IntroductionOAuth2 - Introduction
OAuth2 - Introduction
 
OAuth in the Wild
OAuth in the WildOAuth in the Wild
OAuth in the Wild
 
OAuth 2.0 and OpenId Connect
OAuth 2.0 and OpenId ConnectOAuth 2.0 and OpenId Connect
OAuth 2.0 and OpenId Connect
 
Servlets
ServletsServlets
Servlets
 

Similar a Asp.net mvc filters

Asp.net web api extensibility
Asp.net web api extensibilityAsp.net web api extensibility
Asp.net web api extensibilityEyal Vardi
 
Asp.net mvc internals & extensibility
Asp.net mvc internals & extensibilityAsp.net mvc internals & extensibility
Asp.net mvc internals & extensibilityEyal Vardi
 
Asp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; ExtensibilityAsp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; ExtensibilityEyal Vardi
 
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for Android
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for AndroidApp Optimizations Using Qualcomm Snapdragon LLVM Compiler for Android
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for AndroidQualcomm Developer Network
 
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...Satya Harish
 
Triggers, actions & behaviors in XAML
Triggers, actions & behaviors in XAMLTriggers, actions & behaviors in XAML
Triggers, actions & behaviors in XAMLEyal Vardi
 
Methods and practices to analyze the performance of your application with Int...
Methods and practices to analyze the performance of your application with Int...Methods and practices to analyze the performance of your application with Int...
Methods and practices to analyze the performance of your application with Int...Intel Software Brasil
 
Advanced java programming
Advanced java programmingAdvanced java programming
Advanced java programmingnibiganesh
 
Prism Navigation
Prism NavigationPrism Navigation
Prism NavigationEyal Vardi
 
Vizualytics Execution Management
Vizualytics Execution ManagementVizualytics Execution Management
Vizualytics Execution ManagementMatt Carlucci
 
Utilisation des capteurs dans les applications windows 8
Utilisation des capteurs dans les applications windows 8Utilisation des capteurs dans les applications windows 8
Utilisation des capteurs dans les applications windows 8Intel Developer Zone Community
 
Application Quality with Visual Studio 2010
Application Quality with Visual Studio 2010Application Quality with Visual Studio 2010
Application Quality with Visual Studio 2010Anna Russo
 
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdf
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdfA-Passwordless-Future--WebAuthn-for-Java-Developers.pdf
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdfMohankumarRamachandr1
 
Testing Fiber-Optic Systems
Testing Fiber-Optic Systems Testing Fiber-Optic Systems
Testing Fiber-Optic Systems KHNOG
 
Web api routing
Web api routingWeb api routing
Web api routingEyal Vardi
 
Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?psteinb
 

Similar a Asp.net mvc filters (20)

Asp.net web api extensibility
Asp.net web api extensibilityAsp.net web api extensibility
Asp.net web api extensibility
 
Asp.net mvc internals & extensibility
Asp.net mvc internals & extensibilityAsp.net mvc internals & extensibility
Asp.net mvc internals & extensibility
 
Asp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; ExtensibilityAsp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; Extensibility
 
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for Android
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for AndroidApp Optimizations Using Qualcomm Snapdragon LLVM Compiler for Android
App Optimizations Using Qualcomm Snapdragon LLVM Compiler for Android
 
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...
HH QUALCOMM using qualcomm® snapdragon™ llvm compiler to optimize apps for 32...
 
Triggers, actions & behaviors in XAML
Triggers, actions & behaviors in XAMLTriggers, actions & behaviors in XAML
Triggers, actions & behaviors in XAML
 
Methods and practices to analyze the performance of your application with Int...
Methods and practices to analyze the performance of your application with Int...Methods and practices to analyze the performance of your application with Int...
Methods and practices to analyze the performance of your application with Int...
 
Models
ModelsModels
Models
 
Advanced java programming
Advanced java programmingAdvanced java programming
Advanced java programming
 
Servlet Filters
Servlet FiltersServlet Filters
Servlet Filters
 
Prism Navigation
Prism NavigationPrism Navigation
Prism Navigation
 
File000131
File000131File000131
File000131
 
Vizualytics Execution Management
Vizualytics Execution ManagementVizualytics Execution Management
Vizualytics Execution Management
 
OpUtils webinar
OpUtils webinarOpUtils webinar
OpUtils webinar
 
Utilisation des capteurs dans les applications windows 8
Utilisation des capteurs dans les applications windows 8Utilisation des capteurs dans les applications windows 8
Utilisation des capteurs dans les applications windows 8
 
Application Quality with Visual Studio 2010
Application Quality with Visual Studio 2010Application Quality with Visual Studio 2010
Application Quality with Visual Studio 2010
 
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdf
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdfA-Passwordless-Future--WebAuthn-for-Java-Developers.pdf
A-Passwordless-Future--WebAuthn-for-Java-Developers.pdf
 
Testing Fiber-Optic Systems
Testing Fiber-Optic Systems Testing Fiber-Optic Systems
Testing Fiber-Optic Systems
 
Web api routing
Web api routingWeb api routing
Web api routing
 
Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?
 

Más de Eyal Vardi

Smart Contract
Smart ContractSmart Contract
Smart ContractEyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipesEyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Eyal Vardi
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModuleEyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xEyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationEyal Vardi
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 ArchitectureEyal Vardi
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xEyal Vardi
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 ViewsEyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injectionEyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationEyal Vardi
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScriptEyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 PipesEyal Vardi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 

Más de Eyal Vardi (20)

Why magic
Why magicWhy magic
Why magic
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModule
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

Asp.net mvc filters

  • 1. MVC Filters Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 2. Agenda  What is Filters?  Built-in Filters  Filter Interfaces  Custom Filters  Filter Providers © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 3. ASP.NET MVC Filters  Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods. [HandleError] [Authorize] public class CourseController : Controller { [OutputCache] [RequireHttps] public ActionResult Net( string name ) { ViewBag.Course = BL.GetCourse(name); return View(); } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 4. Filter Interfaces Action Method Action Result 1 2 3 4 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 5. Controller Context 1 6 2 3 4 5 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 6. Filter Order  Filters run in the following order:  Authorization filters  Action filters  Response filters  Exception filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 7. Controller & Filters  The Controller class implements each of the filter interfaces.  You can implement any of the filters for a specific controller by overriding the controller's On<Filter> method.  OnAuthorization  OnActionExecuting  OnActionExecuted  OnResultExecuting  OnResultExecuted  OnException © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 8. “Install” Filters  You can “Install” a filter in the following ways:  Attribute on Actions or Controllers  Add to Global Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 9. IAuthorizationFilter  Make security decisions about whether to execute an action method.  AuthorizeAttribute  RequireHttpsAttribute © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 10. Authorize Attribute Sample  Authorization won’t be granted unless both conditions are met, Users & Roles. [Authorize(Users=“Eyal, Oz”, Roles=“Admin”)] public class CourseController : Controller { public ActionResult Net( string name ) { ViewBag.Course = BL.GetCourse(name); return View(); } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 11. Custom Authorization Policy public class MyAuthorizationAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { return httpContext.Request.IsLocal || AuthorizeCore(httpContext); } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 12. IActionFilter Interface  OnActionExecuting  Runs before the action method.  OnActionExecuted  Runs after the action method © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 13. IActionFilter Context’s © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 14. IResultFilter Interface  OnResultExecuting  Runs before the ActionResult object is executed.  OnResultExecuted  Runs after the result.  Can perform additional processing of the result.  The OutputCacheAttribute is one example of a result filter. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 15. IResultFilter Context’s © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 16. IExceptionFilter  Execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline.  Can be used for logging or displaying an error page.  HandleErrorAttribute is one example of an exception filter. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 17. Handle Error Attribute  You can specify an exception and the names of a view and layout.  Works only when custom errors are enabled in the Web.config file  <customErrors mode="On" /> inside the <system.web>  The view get HandleErrorInfo © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 18. Custom Exception Filter public class MyExceptionAttribute: FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (!filterContext.ExceptionHandled && filterContext.Exception is NullReferenceException) { filterContext.Result = new RedirectResult("/MyError.html"); filterContext.ExceptionHandled = true; } } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 19. Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 20. First Global Controller Action Last Filter I Filter I Filter I Filter I Filter I Filter II Filter II Filter II Filter II Filter II Action Method Action Result Authorization Action Result Exception Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter II Filter II Filter I Filter I Filter I Filter I Filter II Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter I © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 21. Custom Filter  You can create a filter in the following ways:  Override one or more of the controller's On<Filter> methods.  Create an attribute class that derives from ActionFilterAttribute or FilterAttribute.  Register a filter with the filter provider (the FilterProviders class).  Register a global filter using the GlobalFilterCollection class. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 22. Custom Attribute Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 23. Custom Global Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 24. Filter Providers  By default, ASP.NET MVC registers the following filter providers:  Filters for global filters.  FilterAttributeFilterProvider for filter attributes.  ControllerInstanceFilterProvider for controller instances. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 25. The Filter Provider Interface  The GetFilters method returns all of the IFilterProvider instances in the service locator. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il