0

I've created my login using simple session element. I need a logout process as simple as i did. How can i create Log-out in harmony with my Log-in codes? Thanks in advance.

Here is my model below;

public class LoginModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string ErrorMessage { get; set; }
}

and also my controller is below;

 public ActionResult Index()
    {
        Session["User"] = null;
        LoginModel model = new LoginModel();
        return View(model);
    }


    [HttpPost]
    public ActionResult Index(LoginModel data)
    {
        string user = System.Configuration.ConfigurationManager.AppSettings["UserName"];
        string password = System.Configuration.ConfigurationManager.AppSettings["Password"];
        if (data.UserName == user && data.Password == password)
        {
            Session["User"] = "1";
            return RedirectToAction("Index", "Home");
        }
        else
        {
            data.ErrorMessage = "Incorrect password or username entered. Please try again.";
            return View(data);
        }
    }
tereško
  • 56,151
  • 24
  • 92
  • 147

1 Answers1

0

If your definition of "the user is logged in" is "The UserID is stored in Session["User"]", then logging out is equally trivial: just clear the session variable, as explained in ASP.NET removing an item from Session?:

[HttpPost]
public ActionResult LogOut()
{
    Session.Remove("User");
}
Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236