2016-03-23 56 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

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