ASP.NET MVC id: multiple login paths and changing the default login path in controller
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 to share
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 to share