How not to allow the user to access the previous login/registration page after logging in or registering

Asked

Viewed 901 times

1

I am developing an ASP.NET application and when the user logs in and clicks on the browser button to go back he can access the login/registration page, even if I check the logged-in user’s key on Session. Follow the prints:
1 - Fiz login Eu faço login

2 - I am logged in, the site redirects me to the index and I click the back button. Index e clico em voltar

3 - After that I go back to the registration/login screen which can not happen: inserir a descrição da imagem aqui

After photo 3, if I go back to index, I log in normally, to prevent this I did a check in the controller checking whether the user is logged in or not, if yes it goes back to index, but when the user clicks back it looks like the browser that does this control. I’ve seen sites that don’t allow you to go back to the login screen, does anyone know how to fix it? Follow the check in the controller:

    public ActionResult LoginRegister()
    {
        if (Session["UserStatus"] != null)
        {
            return RedirectToAction("Index", "Home");
        }

        return View();

    }

    [HttpPost]
    public ActionResult LoginRegister(string fr,string t,string ReT)
    {
        if (Session["UserStatus"] != null)
        {
            return RedirectToAction("Index", "Home");
        }
        //Validação e outros processos.
    }

2 answers

1


Just add in the login page builder to not store cache, I believe this solves your problem.

For . net 4x.

[OutputCache(NoStore = true, Duration = 0)]
public ActionResult LoginRegister()
    {
        if (Session["UserStatus"] != null)
        {
            return RedirectToAction("Index", "Home");
        }

        return View();

    }

For . net core use:

[ResponseCache(NoStore = true, Duration = 0)]
 public ActionResult LoginRegister()
        {
            if (Session["UserStatus"] != null)
            {
                return RedirectToAction("Index", "Home");
            }

            return View();

        }

0

Use the Actionfilterattribute

First create a Loginfiltro class (for example):

public class LoginFiltro : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        object usuarioLogado = filterContext.HttpContext.Session["Nome"];

        if (usuarioLogado == null)
        {
            filterContext.Result = new RedirectToRouteResult(
                      new RouteValueDictionary(
                          new { action = "Index", controller = "Login" }));
        }
    }
}

which checks whether the user is logged in or not. Otherwise, the user will be redirected to the Controller Login.

No Controller

Then "decorate" the controllers you want to have this login control with:

[LoginFiltro]

above the Controller name.

  • Cassio will apply your solution soon return with the result, VLW

Browser other questions tagged

You are not signed in. Login or sign up in order to post.