Asp.net MVC: redirect to action in custom action attribute

I am trying to create an attribute that I can set for my action with controllers in which I can check if the user is any role. I know with standard asp.net authentication, this is already possible, but I do not use this.

So what I want to do is:

[Role("Admin", "SuperUser")]
public ActionResult SafeCourse()

I built this:

public class AdminAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

Ans now ..... I want to redirect the user to some kind of controller / action or if it is impossible for some presentation when he / she is not in the right role. But I'm not in my usual context (= controller), and I'm somewhat lost :)

Michelle

+3
source share
2 answers

IAuthorizationFilter, ... OnAuthorization(AuthorizationContext filterContext) .

-

filterContext.Result = new RedirectResult("/Controller/ActionYouWantToDirectTo");

,

Dan

+2

/

Asp.net MVC . , , .

public class RoleAttribute: ActionMethodSelectorAttribute
{
    ...
}

, . Asp.net MVC, .

0

Source: https://habr.com/ru/post/1728641/


All Articles