Introduction
ASP.NET MVC supports various types of Authorization /Authentication that are builtin.
But sometimes we would like to customize it for project requirements.
Before going to the authorization process in depth let's have a look at Authentication and Authorization. Actually there is a bit of a misconception about them.
The following are the differences in short:
- Authentication: It is a process of verification that verifies “Who you are” (it confirms that you are a valid (or invalid) user).
- Authorization: It is a process of verification that verifies “What to do” (It confirms you are permissible to do (or not to do) that).
In the MVC framework there are filters that execute in sequence. The sequence is:
- Authorization Filters
- Action Filters
- Result Filters
- Exception Filters
It's clear that Authorization filters are taking care of authorizing the current user.
How Authorize Attribute Works
If you are using the ASP.NET membership provider for authentication then it's quite easy to use Authorization in MVC. Here is an example.
- [Authorize(Users = "anupam,ashwin")]
- public ActionResult AddArticle()
- {
- return View();
- }
We can also specify Roles instead of Users.
Defining Custom Attribute for Authorization
The AuthorizeAttribute Class is defined as:
- [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true,
- AllowMultiple = true)]
- public class AuthorizeAttribute : FilterAttribute,
- IAuthorizationFilter
- <>{
- public AuthorizeAttribute()
- {…}
- protected virtual bool AuthorizeCore(HttpContextBase httpContext)
- {…}
- public virtual void OnAuthorization(AuthorizationContext filterContext)
- <>{…}
- protected void HandleUnauthorizedRequest(AuthorizationContext filterContext)
- <>{…}
- .
- .
- .
- }
Note: another method and properties are omitted to keep it simple.

sagar deshmukhPosted Feb 21, 2021, 3:09 AM
Would you please also explain .. In MVC we store authentication information in Session. Same where we store token's information n web API for next call. Means there are multiple users have multiple tokens. Would you please provide an article for the same. Thank you in Advance.
sagar deshmukhPosted Feb 21, 2021, 3:04 AM
Hi Anupam,
Hiren JoshiPosted Oct 31, 2017, 6:59 AM
Thank you Nice... superb
Hamid KhanPosted Sep 18, 2017, 8:21 AM
Nice..................
Sagar PrakashPosted Sep 14, 2017, 1:58 AM
Thank you for the article..I want to know how OnAuthorization method of AuthorizeAttribute is used in CustomAuthorization class.
Michael OwenPosted Sep 10, 2017, 6:53 AM
This is nice Article! But what if I don't want to use AuthorizeAttribute ? Here is my version of custom authorization using Sessions : https://www.youtube.com/watch?v=kRcjE4YNXQU
Buddy LopezPosted Jul 3, 2017, 2:01 AM
Hi, if i want to add option "Sign In different User" using the form authentication but you still need to check the windows authentication. How can we implement this? Do you have sample codes? Thanks
Vanama DineshPosted May 4, 2017, 6:09 AM
Hi, how can i get current user and what is context (GetUser.CurrentUser) var user = context.AppUser.Where(m => m.UserID == GetUser.CurrentUser/ please help out with the following
krissh fosterzPosted Feb 4, 2017, 6:28 AM
Does everytime time it hits the db to get check authorized user
Brien KingPosted Jan 23, 2017, 1:06 PM
Is there a way to override the actual [Authorize] attribute or do I need to create the derived class and decorate my views with that name instead? In other words, if all my views are already decorated with [Authorize], can I write something that replaces the normal controller for doing the authorization?
Muhammad SabriPosted Nov 6, 2015, 12:17 AM
Can I get downloadable code?
Piyush DixitPosted Oct 22, 2015, 8:45 AM
Nice articales
Prasad MangaonkarPosted Aug 24, 2015, 1:45 AM
is there any way to implement without using the ASP.NET membership provider for authentication????
Tyler beardPosted Aug 20, 2015, 3:30 PM
Is there a way for an integration test to "hit" this custom attribute?
Mohamed JawadPosted May 8, 2015, 2:55 AM
This is just what I was looking for. But I am having trouble implementing it. What is the 'context'? What can be the class in my application to create 'context'? Is it the service from which I get all the user info after authentication? Thanks!
Anupam SinghPosted Apr 8, 2015, 1:10 AM
let me know if you face any issue with it
Anupam SinghPosted Apr 8, 2015, 1:10 AM
Hi vali, thax for your feedback. actually you just need to create a class same as CustomAuthorizeAttribute which is mentioned in above code sample, and use it like [CustomAuthorize(“Administrator”,”Moderator”) ] in your action methods. your business logic should be in : bool AuthorizeCore(HttpContextBase httpContext) method.
vali khanPosted Apr 7, 2015, 5:22 AM
Can plz provide the code
vali khanPosted Apr 7, 2015, 5:21 AM
Very good article