I wanted to disable authentication throughout the application during debugging, I did the following:
1) Created this class.
namespace System.Web.Mvc { public class SwitchableAuthorizeAttribute : AuthorizeAttribute { public static bool Enabled = true; public override void OnAuthorization(AuthorizationContext filterContext) { if (Enabled) { base.OnAuthorization(filterContext); } } } }
2) Replaced the [Authorize] attribute with [SwitchableAuthorize] throughout the application.
3) If necessary, disable authorization. For example, I added the following to App_Start / AuthConfig.cs:
public static class AuthConfig { public static void RegisterAuth() { #if DEBUG SwitchableAuthorizeAttribute = false; #endif ... } }
You may have conditions other than DEBUG. This approach allows you to programmatically enable / disable authorization at any time.
If your pages require entering user information, this method can be improved by doing some dummy login and not just skipping base.OnAuthorization ().
source share