How To Develop Custom Action Filter Attribute In Asp.Net MVC Development

The article is written by Aegissofttech developer in order to share information about creating custom Action Filter Attribute at asp.net MVC development platform. The development team is sharing its experience with global community, so that other country’s developers can learn from them and make best practices of asp.net development.

Develop Custom Action Filter Attribute

MVC Authentication using Filter Attribute:

Create Action Filter class:
An action filter consists of codes that run either before or after an action runs. It can be used for tasks like logging, privileged based authorization, authentication, caching etc.

How to create custom Action Filter Attribute:

  • Create a class “LoginAuthAttribute”
  • Inherit that class with “ActionFilterAttribute, IAuthenticationFilter”
  • Override “OnAuthenticationChallenge” method.
 public class LoginAuthAttribute : ActionFilterAttribute, IAuthenticationFilter  
 {  
 public void OnAuthentication(AuthenticationContext filterContext)  
 {  
 }  
 public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)  
 {  
 var user = filterContext.HttpContext.User;  
 if (user == null || !user.Identity.IsAuthenticated)  
 {  
 filterContext.Result = new HttpUnauthorizedResult();  
 }  
 }  
 }  

You can see above code, the “OnAuthencitcationChallenge” method is overridden, we want to execute this method before any action is executed.

In above code as you can see, we check the user is authenticated or not, if user is not authenticated then we set Result as a UnAuthorisezed result.

Set form authentication mode in web config file as per below code.

 <authentication mode="Forms">  
 <forms loginUrl="~/Login" timeout="2880" />  
 </authentication>  

To use LoginAuthAttribute in our controller we are creating a new BaseController. So, others controller’s is this controller are inherited by this BaseController.

 [LoginAuthAttribute]  
 public class BaseController : Controller  
 {  
 }  

Design Login form.

Create a Login controller and implement login method.

 [HttpPost]  
 public ActionResult Login(LoginModel model, string ReturnUrl)  
 {  
 FormsAuthentication.SetAuthCookie(model.UserName, false);  
 if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")  
 && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))  
 {  
 return Redirect(ReturnUrl);  
 }  
 else  
 {  
 return RedirectToAction("Index", "Home");  
 }  
 }  
 public ActionResult Logout()  
 {  
 FormsAuthentication.SignOut();  
 return RedirectToAction("Index", "Login");  
 }  

Create two method as per above code Login and Logout in Login Controller.

Create a HomeController and inherit this controller with “BaseController”

 public class HomeController : BaseController  
 {  
 }  

Now each and every action methods of Home Controller checks the user is authenticate of not before execute. If user is not authenticate then this will redirect to Login page.

Conclusion:

In the above document we learned how to create custom attribute. Also learned how to use custom attribute with controller in MVC.

Author of this article is working with the Aegissofttech asp.net MVC development team. The post is published by expert to assist global developers and help them in making best development practices. To get more info on related topic, you can contact the Aegis experts anytime, any day, from anywhere. They will get in touch with you soon.

Author bio- Rohit is working with Aegissofttech since long time. Rohit has vast experience is developing asp.net MVC applications for global clients. This article story is published to let other developers know how they can develop a custom action filter attribute and use it for a project.

Essential Frameworks For Trailblazing Java Web Development Solutions

The enterprises using java apps and java web development frameworks are increasing every day. But the problem is that few enterprises are still using conventional web frameworks and they are not pretty much sure about the best web development framework. Even experts are not sure about best framework as there are plenty of choices available and all of them have some unique and interesting features.

In brief we can say web arena is facing a broad change due to technology advancements, market trends, and experts’ choice. This is the reason after deep research and expensive hands-on experience in Java; we have concluded 10 most popular web development frameworks that will help you in making the right decision.

Let us discuss on all of these frameworks for trailblazing web development solutions one by one :

  1. Struts 2

The robust web development framework has been designed to build enterprise web apps from sourcing to deployments. The framework is result for combined efforts of Strut community and WebWork. The best part is that it can create dynamic responses.

“Struts 2” is an improved version of previous one that assures clean code for web apps. It uses AJAX tags to create flexibility and making web apps more interactive. The Java developers who wants to play with open source code, struts 2 is recommended option.

  1. JSF

JSF is used to prepare server side interfaces for web applications. JSF is based on component oriented approach not on object oriented that makes java web development easy and more robust. it can be integrated with other Java frameworks on request.

Advantages

  • Enterprise app development is easy and robust
  • JSF is considered fast as compared to server side interfaces
  • There is plenty of component libraries to help you
  1. Spring MVC

Spring MVC is works on presentation layer and applicable on wide product range. When working with Spring MVC, you can use objects everywhere, there is need to implement any base class. It also allows flexible data binding. it is taken as strategic and flexible web platform for Java web apps.

  1. Wicket

Wicket is a light weight component for designing robust java apps. It is suitable for apps that require high end GUI interface. It quickly distinguish business and application layer for eat template editing. this is a component based mode where component has its own state and model. if developers wanted to make most of their resources then Wicket can be consider best web development framework available so far.

  1. Stripes

This framework allows interaction between complex data objects. it support data binding, data validation, and data conversion. It is generally used to manage large volume of data that is highly complex in nature and difficult to manage by experts. Once you have registered for this web framework then it does not need for any external configuration. We can say stripes offer easy and efficient solution of complex problems in a web tier. This is a compact web framework with few dependencies.

  1. Tapestry

This is an open source and object oriented framework for designing powerful Java apps. It is based on MVC i.e. Model – View – Controller method and allows quick implantation of apps. The framework is much focused on methods, queries and functions instead of queries and URLs. It is based on modular approach for web development where UI on web pages are combined with their corresponding Java classes.

  1. RIFE

In this framework, each small is focused and approached carefully. A single app is divided into different modules; each developer has its own task so that it can be completed successfully. Whenever it is needed, one module can be effortlessly integrated with other one that is called seamless integration of modules. In this way, it is easy for developers to manage code, code replication and redundancies can also be reduced significantly.

  1. SEAM

This is an open source web framework suitable for designing internet rich applications. It integrates different technologies together like AJAX, Business Process Management (BPM), JSF, Java beans etc. it also deals with contexts basics. Web development is easy with JBOSS tools and design issues are also eliminated to a large extent. Some of highlights of SEAM include – Emailing, PDF creation, graph designing, worksheet formation etc.

  1. Google Web toolkit

GWT is a web development framework that allows developers to maintain and build Java apps. it would be easy for you to implement Google maps and Gmail with Google web toolkit. There are plenty of tools that make web development easy for developers. There is also AJAX assistance that allows them to write applications quicker. You can also test, share or edit AJAZ apps whenever necessary.

  1. OpenXava

This is a web development framework for designing AJAX apps at enterprise level. It helps developer in designing full – fledged Java apps that ready to market. It allows easy report generation and module generation. This is much faster as compared to MVC, Ruby etc.