How to stop the user on logins / registrations and other non-authenticated pages in the MVC3 application?

As soon as the user logs in to my site, where I use Authentication, then I can stop the user on the login and registration page if he has allready login and register.

+6
source share
3 answers

Two ways "from the head":

1 - Custom Action Filter , which redirects the user from the page if they are logged in.

 public class RedirectAuthenticatedRequests : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if(filterContext.HttpContext.Request.IsAuthenticated) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary(new { controller = "SomeController", action = "SomeAction" } )); } base.OnActionExecuting(filterContext); } } 

2 - A simple check in the login action method if the user is logged in.

 if(Request.IsAuthenticated) return RedirectToAction("SomeOtherView"); 
+5
source

An easy way out is a check in the controller method (login / register) if the user is authenticated and if he redirects the user to the desired page:

Something like this for the login page (same with Register):

 // // GET: /Login/Index public ActionResult Index() { if(User.Identity.IsAuthenticated){ //redirect to some other page return RedirectToRoute("Home", "Index"); } return View(); } 
+5
source

You can check the User.Identity.IsAuthenticated property and redirect them accordingly.

0
source

Source: https://habr.com/ru/post/910947/


All Articles