The native ASP.Net Membership provider has attributes that we can use to decorate Actions and set permissions. But you may want to extend this security so this article will describe how you can do that.
Let's say we have an administrative Controller called UsersController. In this controller, there is an Action called Index() which lists all the users in the system.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Mvc;
- using System.Web.Script.Serialization;
- namespace MVCApp.Controllers
- {
- public class UsersController : Controller
- {
- private readonly IViewModelUserBuilder _builder;
- public UsersController(IViewModelUserBuilder builder)
- {
- _builder = builder;
- }
- //
- // GET: /Admin/Users/
- public ActionResult Index()
- {
- var model = _builder.GetAllUsers();
- return View(model);
- }
- }
- }
You might think this is fine, but think again. What happens if someone directly types into their browser <a href="#">http://mywebsite/users</a>? They will get to see this view and all the users that are returned from the database in your action. Not good.
What you should do is decorate your action with the [Authorize] annotation. This will ensure that the user is authenticated before they can access this view.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Mvc;
- using System.Web.Script.Serialization;
- namespace MVCApp.Controllers
- {
- [Authorize]
- public class UsersController : Controller
- {
- private readonly IViewModelUserBuilder _builder;
- public UsersController(IViewModelUserBuilder builder)
- {
- _builder = builder;
- }
- //
- // GET: /Admin/Users/
- public ActionResult Index()
- {
- var model = _builder.GetAllUsers();
- return View(model);
- }
- }
- }
But this still does not guarantee security. You may only want Administrators to see this list of users. If you are using the built-in .Net security model, can you extend the [Authorize] annotation to include security group names. The code below will only allows users in the "Administrators" group to see the content of this view. But this requires that you use the built-in security model and I (generally) do not. I have my own builders and Membership methods that I use instead (I will share these methods with you if you want. Just let me know via the comments below). Instead, I customised the Authorize attribute to suit MY data store and builders.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Mvc;
- using System.Web.Script.Serialization;
- namespace MVCApp.Controllers
- {
- [Authorize("Administrator")]
- public class UsersController : Controller
- {
- private readonly IViewModelUserBuilder _builder;
- public UsersController(IViewModelUserBuilder builder)
- {
- _builder = builder;
- }
- //
- // GET: /Admin/Users/
- public ActionResult Index()
- {
- var model = _builder.GetAllUsers();
- return View(model);
- }
- }
- }

Gowtham RajamanickamPosted May 26, 2015, 9:02 AM
Good one..
Karthik Muthu KaruppanPosted Apr 23, 2015, 11:16 AM
nice
Santhakumar MunuswamyPosted Apr 21, 2015, 9:41 AM
Thanks for nice article
Rahul Kumar SaxenaPosted Apr 21, 2015, 8:19 AM
Good Work..
NitinPosted Apr 21, 2015, 5:42 AM
good one