2015-09-28 39 views
0

首先,我是一個ASP.NET新手,所以很抱歉,如果這個問題是愚蠢的!ASP.NET MVC4 LogOff不工作

如果用戶名/密碼與數據庫數據匹配,我已經創建了一個使用會話變量的登錄系統! 的問題是,雖然即時通訊能夠登錄我不能退出

// 
    // POST: /Account/LogOff 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult LogOff() 
    { 
     Session.RemoveAll(); 
     return RedirectToAction("Index", "Home"); 
    } 

上面的代碼是從所述的AccountController

@if (Session["LoggedUser"]!=null) { 
<text> 
    Hello, @Html.ActionLink(Session["Username"].ToString(), "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })! 
    @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) { 
     @Html.AntiForgeryToken() 
     <a href="javascript:document.getElementById('logoutForm').submit()">LogOff</a> 
    } 
</text>} else { 
<ul> 
    <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li> 
    <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li> 
</ul>} 

和上述距離_partialLogin

我的問題是,當我按logOff該站點只是跳過控制器的LogOff部分,並且會話未清除,這意味着即時消息仍然以用戶身份登錄 感謝您的幫助

編輯: 這裏是登錄頁面的情況下,控制器是需要:

// 
    // GET: /Account/Login 

    [AllowAnonymous] 
    public ActionResult Login(string returnUrl) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 

    // 
    // POST: /Account/Login 

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Login(LoginModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      DefaultConnection dc = new DefaultConnection(); 
      var Users = (from c in dc.NonActivated_Users select c).ToList<NonActivated_Users>(); 
      foreach (NonActivated_Users nua in Users){ 
       if (nua.Password_Hash == Hasher.HashString(model.Password) && nua.Username==model.UserName){ 
        Session["LoggedUser"] = nua; 
        Session["Rights"] = 4; //non activated user 
        Session["Username"] = 0; 
        nua.LastActive = DateTime.Now; 
        dc.SaveChanges(); 
        return RedirectToLocal(returnUrl); 
       } 
      } 
      var Users1 = (from c in dc.User select c).ToList<User>(); 
      foreach (User au in Users1) 
      { 
       if (au.Password_Hash == Hasher.HashString(model.Password) && au.Username == model.UserName) 
       { 
        Session["LoggedUser"] = au; 
        if (au.Membership == false) { 
         Session["Rights"] = 3; //activated user non premium 
        } 
        else 
        { 
         Session["Rights"] = 2; //activated user premium 
        } 
        au.Last_Active = DateTime.Now; 
        dc.SaveChanges(); 
        return RedirectToLocal(returnUrl); 
       } 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     return View(model); 
    } 

回答

1

您沒有設置FormsAuthentication餅乾,出於這個原因,你不能達到你的註銷等動作。

您將需要[AllowAnonymous]屬性,或在成功登錄設置身份驗證cookie來裝飾你的註銷等動作

FormsAuthentication.SetAuthCookie(user.Username, false); 

編輯:

我建議你檢查用戶身份驗證User.Identity.IsAuthenticated,而不是檢查是否存在會話。 除此之外,您可以將會話變量存儲到單個自定義對象,然後將該對象存儲到單個會話中。在項目的後期階段跟蹤你的會話會更容易:)

+0

你真了不起!謝謝:) –

+0

歡迎:) – Robert