Middleware with ASP.NET MVC and . NET 4.5

Asked

Viewed 515 times

1

It is possible to add some middleware the routes that will, for example, allow or redirect a user to access the admin panel, which if not authenticated will be redirected to the login area? as in the example below (In PHP / Laravel):

Route::get('/login', [
    'as' => 'login',
    'uses' => 'Auth\LoginController@Index',
    'middleware' => ['guest', 'throttle:12,1'],
]);

My routes on ASP.NET:

routes.MapRoute(
    name: "Admin.Login", url: "admin/login",
    defaults: new { controller = "Authentication", action = "Index" },
    namespaces: new[] { "Controllers.Admin.Authentication" }
);

routes.MapRoute(
    name: "Admin.Login.Attempt", url: "admin/login/attempt",
    defaults: new { controller = "Authentication", action = "Attempt" },
    namespaces: new[] { "Controllers.Admin.Authentication" }
);

Note: my question is about routes in ASP.NET, the code section in PHP is only to illustrate my question.

  • What would that "middleware"?

  • Middleware is an intermediate class between Route and Controller, where we can, for example, check if the user is authenticated when trying to access the admin panel, and if not, middleware returns a redirect to a defined route, as the login, preventing the user from proceeding without logging in.

1 answer

1


In ASP.NET MVC there is no such concept of "middleware". What exists are user architectures and authentication that can be embedded into the application, which is an approach that I believe is close to middleware of Laravel.

Authentication is usually conferred through attributes, which can be native or written by the application programmer. To write your own attribute or understand how authentication is done, you can start by the answers here on the site or go to the tutorials for the main user architecture and ASP.NET MVC authentication, Identity.

There are also other architectures, older or developed by third parties, like ASP.NET Membership or the Identity Manager.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.