M
M
MicroSLAV2015-03-23 00:04:02
ASP.NET
MicroSLAV, 2015-03-23 00:04:02

How to execute code before passing it to controller in ASP.NET?

Good day.
There is a task to check authorization data at the user. The request looks like this
/user_id/token/controller/function/argument1/argument2
It is necessary to execute a method that checks id and token before the function is called in the controller (that is, before calling the View). How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wkololo_4ever, 2015-03-23
@MicroSLAV

public class AuthorizeUserAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
    if(  httpContext.Request.RequestContext.RouteData.Values["token"]&&httpContext.Request.RequestContext.RouteData.Values["id_пользователя"])//ваша логика проверки на валидность
{
 return true;
}
 return false;
    }
}

[AuthorizeUser]
    public ActionResult ВашеДействие()
    {
        //...
        return View();
    }

[AuthorizeUser]
    public class TestController 
    {
        //...
    }

PS: Please read any literature on ASP.NET MVC, because "Need before a function call occurs in the controller (that is, before accessing the View)" sounds terrible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question