2016-03-23 51 views
0

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

下面是我的模型;

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

回答

0

如果您的定义“用户登录”,“用户ID存储在Session["User"],然后注销同样简单:只需清除会话变量,如ASP.NET removing an item from Session?解释说:

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

有用的信息。谢谢您的回答。 –