Once user logins to my site where i use Form Authentication then how can i stop user to going on Login & Register page if he has allready login & register.
Two ways "off the top of my head":
1 - Custom Action Filter
that 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 - Simple check in the login
action method if the user is logged in.
if(Request.IsAuthenticated) return RedirectToAction("SomeOtherView");
The easy way out is checking in the controller method(login/register) if the user is authenticated, and if it is redirect the user to the page you want:
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With