How to configure 2 actions with the same name, 1 Authorized and 1 not authorized?

Is it possible to have something like this in ASP.NET MVC ...

[Authorize] [AcceptVerbs(HttpVerbs.Get)] public string AddData(string Issues, string LabelGUID) { return "Authorized"; } [AcceptVerbs(HttpVerbs.Get)] public string AddData() { return "Not Authorized"; } 

So, if the user is not logged in, he uses an unauthorized action by default.

+3
source share
2 answers

Yes it is possible. You will need to create your own ControllerActionInvoker and override the FindActionMethod member. I would suggest that the base class work, and then check if the method that it returns matches your criteria, and if not, return the best match.

I am doing something like this to allow my controllers to have a "default action" and it works well. Check out MvcContrib and their implementation of their ActionInvoker for a really nice example.

+3
source

I don’t believe that. The controller action with the best match for the parameters will be selected, and then the attributes will be applied.

you can use

 if (Request.IsAuthenticated) { return "Authorized"; } else { return "Not Authorized"; } 

Under the hood [Login] essentially the same thing is done

 protected virtual bool AuthorizeCore(IPrincipal user) { if (user == null) { throw new ArgumentNullException("user"); } if (!user.Identity.IsAuthenticated) { return false; } ...snip... } 
+1
source

All Articles