Using Asp.Net MVC 5 and Identity v2, I have a controller action like:
[Authorize]
public ActionResult SemiSecureAction(string parameter)
{
var model = SomeService.InitModel(parameter);
// some controller logic
return View(model);
}
I need to have that [Authorize]
attribute on this action only if the parameter meets a condition. (e.g. !String.IsNullOrEmpty(parameter)
)
That is, I need to authorize a user only if the action's parameter meets a condition, and I need to have the same behavior in that case as the [Authorize]
attribute does (e.g. redirect to login page with a returnUrl
, etc.).
How can I do this? How can I use MVC controller annotations conditionally?