简体   繁体   中英

ASP.NET MVC - How do Remove log in Cookies from a asp.net site

How to avoid that when logging in, the browser saves this login data and the user can enter even after closing the browser

Thats my login controller

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        var userDTO = new ExpandedUserDTO();

        switch (result)
        {                            
            case SignInStatus.Success:
                ApplicationUser user = await UserManager.FindAsync(model.Email, model.Password);
                return RedirectToAction("RedirectLogin");

            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Falha ao Realizar login, usuário ou senha incorretos.");
                return View(model);
        }
    }

The third parameter to SignInManager.PasswordSignInAsync determines whether or not the cookie will persist after the browser is closed. If you don't want the cookie to persist, always pass in false for this parameter.

See the documentation for this method here - https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1.passwordsigninasync?view=aspnetcore-2.2

You could try making the following changes:

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, false, shouldLockout: false);
    var userDTO = new ExpandedUserDTO();

    switch (result)
    {                            
        case SignInStatus.Success:
            ApplicationUser user = await UserManager.FindAsync(model.Email, model.Password);
            return RedirectToAction("RedirectLogin");

        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Falha ao Realizar login, usuário ou senha incorretos.");
            return View(model);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM