Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Identity: Multiple Login Path and modify the default Login Path in controller

How can I change the default authentication redirection path (/Account/Login) for controllers ? e.g I got 4 controllers

ABC --> /ABC/Login

BCD --> /BCD/Login

EFG --> /EFG/Login

Home ---> Account/Login

like image 903
user2376512 Avatar asked Dec 19 '22 11:12

user2376512


1 Answers

Here is a custom Authorize attribute, as per Chris Pratt's idea:

public class CustomAuthorize:AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        string controller = filterContext.RouteData.Values["controller"].ToString();
        filterContext.Result = new RedirectToRouteResult(new
        RouteValueDictionary(new{ controller = controller, action = "Login" }));
    }
}

Can be used on your controller like this:

[CustomAuthorize]
public class ABCController : Controller

This will redirect an unauthorized client to the Login action on the controller it's trying to access. Remember to put [AllowAnonymous] on your Login actions.

like image 111
Tobias Avatar answered May 03 '23 06:05

Tobias