【问题标题】:Log-out operation in MVCMVC中的注销操作
【发布时间】:2016-03-23 20:51:44
【问题描述】:

我使用简单的会话元素创建了我的登录。我需要一个像我一样简单的注销过程。如何创建与我的登录代码一致的注销?提前致谢。

下面是我的模型;

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

我的控制器也在下面;

 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);
        }
    }

【问题讨论】:

    标签: asp.net-mvc session logout


    【解决方案1】:

    如果您对“用户已登录”的定义是“用户ID存储在Session["User"],那么注销同样简单:只需清除会话变量,如ASP.NET removing an item from Session? 中所述:

    [HttpPost]
    public ActionResult LogOut()
    {
        Session.Remove("User");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2013-04-07
      • 2013-01-26
      • 2019-01-03
      • 1970-01-01
      • 2011-05-14
      相关资源
      最近更新 更多