ASP.NET MVC id: multiple login paths and changing the default login path in controller

How can I change the default authentication redirect path (/ Account / Login) for controllers? for example i got 4 controllers

ABC → / ABC / Login

BCD → / BCD / Input

EFG → / EFG / Login

Home ---> Account / Login

+3


source to share


3 answers


Here is the custom Authorize attribute, as suggested by Chris Pratt:

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 the unauthorized client to the Login action on the controller it is trying to access. Don't forget to put [AllowAnonymous]

on Login Actions.

+4


source


You can remove the attribute Authorize

and check IsAuthenticated

inside the action Details

. if authentication is complete, redirect the login action.



+1


source


according to this answer fooobar.com/questions/2158704 / ...

When using the new OWIN forms authentication (as opposed to the old ASP.NET forms authentication), this is set in the Startup class. In default templates, it is in the App_Start/Startup.Auth.cs

ConfigureAuth method:

public void ConfigureAuth(IAppBuilder app)
{
   app.UseCookieAuthentication(new CookieAuthenticationOptions
   {
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
      //here you can change the login url
      LoginPath = new PathString("/Account/Login")
   });
   app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

      

0


source







All Articles