Regarding the override of OnAuthorization according to the ProASP.NET MVC3 Book, they do not recommend overriding it, since the default implementation of this method safely processes the content cached by the OutputCache filter.
If you are looking for user authentication (using Auth forms) and authorization (using role provider logic, the following describes how I protected my application.
EDIT: The following logic uses integrated forms authentication and role manager. After user authentication and authorization, the user ID can be used for authentication (User.Identity.IsAuthenticated) and User.IsInRole ("admin") roles
In Web.Config:
<authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="15" slidingExpiration="true" enableCrossAppRedirects="false" protection="All" /> </authentication> <roleManager enabled="true" defaultProvider="MyRolesProvider" cacheRolesInCookie="true" cookieProtection="All"> <providers> <clear /> <add name="MyRolesProvider" type="MyApp.Library.CustomRolesProvider" /> </providers> </roleManager>
To authorize a role. Extend the RoleProvider methods and override if necessary.
public class CustomRolesProvider : RoleProvider { public override string[] GetRolesForUser(string username) {
In your controller, you can now use this:
[Authorize(Roles="Admin")] public class AdminController : Controller { .... }
For authentication, I have implemented my own authentication, but I still use forms authentication:
//This one calls by Custom Authentication to validate username/password public ActionResult LogOn(LogOnViewModel model, string returnUrl) { if(Authenticate("test","test")) { ....... } } public bool Authenticate(string username, string password) { //Authentication Logic and Set the cookie if correct else false. //..... your logic.... //..... FormsAuthentication.SetAuthCookie(username, false); }
source share