Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can think of alternative ways to implement this, but they all involve using a customized AuthorizeAttribute. One way to do this would be to have a base controller using said customized AuthorizeAttribute that all of your controllers derive from. This attribute would be customized to prevent anonymous (and unauthorized) access to an action unless it's controller or the action itself has been decorated with another attribute -- say the AnonymousEnabledAttribute. All of your controllers would derive from this controller and thus inherit it's standard "no anonymous by default" behavior. You would then simply decorate the controllers/actions you want to be anonymous with the AnonymousEnabledAttribute -- providing the override for that controller or action. Or, for a controller, simply don't inherit from the protected controller and all of it's actions become public.</p> <p>Oh, and your whole site would have to remain open.</p> <pre><code>[OverridableAuthorize] public abstract class ProtectedController : Controller { } public class MostlyProtectedController : ProtectedController { public ActionResult ProtectedAction() { } [AnonymousEnabled] public ActionResult PublicAction() { } } [AnonymousEnabled] public class ExplicitlyPublicController : ProtectedController { // inherits additional behaviors, but anonymous is enabled by attribute } public class PublicByOmissionController : Controller { // doesn't inherit and is thus public -- assuming whole site is open } public class AnonymousEnabledAttribute : Attribute { } public class OverridableAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization( AuthorizationContext context ) { context.HttpContext.Items["ActionDescriptor"] = context.ActionDescriptor; base.OnAuthorize( context ); } public override bool AuthorizeCore( HttpContextBase context ) { var actionDescriptor = context.Items["ActionDescriptor"] as ActionDescriptor; if (actionDescriptor == null) { throw InvalidOperationException( "ActionDescriptor missing from context" ); } var attribute = actionDescriptor .GetCustomAttributes( typeof(AnonymousEnabledAttribute,true) .FirstOrDefault(); if (attribute == null) { return base.AuthorizeCore( context ); } return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload