簡體   English   中英

瀏覽器后退按鈕的注銷問題

[英]Logout issue with browser back button

我已經使用ASP.Net MVC 4創建了登錄/注銷功能。我使用了自己創建的表單來針對Active Directory驗證用戶身份。 該功能正常運行。

在安全方面仍然存在一個大問題。 用戶單擊注銷鏈接后,他/她成功注銷並再次重定向到登錄表單。 控制器中的代碼如下所示。

    public ActionResult Logout()
    {
        // Tried to include below 3 lines in _Layout.cshtml as well. But not identifying.
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
        Response.Cache.SetNoStore();            

        Session.Abandon();              

        return RedirectToAction("Login");
    }

但是,一旦單擊瀏覽器后退按鈕,用戶就可以返回其他頁面並瀏覽頁面。

我經歷了幾種解決方案,不同的方法,但都沒有解決。 看來MVC方法與ASP.NET表單有很大不同。 感謝您的幫助。

(我正在尋求使用C#/ MVC方式解決此問題。不使用JavaScript在注銷時禁用/關閉瀏覽器。)

更新:代碼片段

    [HttpPost]
    public ActionResult Login(LoginModel authUser)
    {
        // Call Helper to get LDAP info. Will return username with groups or null      
        UserModel userProfile = LdapLoginHelper.AuthenticateUser(authUser);

        if (userProfile != null)
        {                
            Session["UserName"] = userProfile.UserName;
            Session["LdapGroups"] = userProfile.LdapGroups;

            if (userProfile.LdapGroups.Contains("Administrators"))
            {
                // To be implemented                   
            }
            else
            {
                // To be implemented      
            }

            // Successful login. Redirect to main page
            return RedirectToAction("Home", "Home");
        }
        else
        {
            // Invalid Login. Redirect to Login page
            return RedirectToAction("Login");
        }            
    }



    public ActionResult Logout()
    {
        // Not worked
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Session.Abandon();

        /// Tried this too. Not worked.
        /// Session.Clear();
        /// FormsAuthentication.SignOut();

        //// Tried this also. Not worked.
        //// WebSecurity.Logout();

        return RedirectToAction("Login");
    }

除此常見的_Layout.cshtml頁面標題外,如下所示。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
.
. 
.

在global.asax頁面中添加以下代碼,並從logout()函數中刪除前三行。

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}

我只將SetExpires與DateTime一起使用。現在,它將使您的本地服務器時間與Cookie匹配。 使用DateTime.UtcNow.Addminutes(-1)可能是真正的罪魁禍首。

另外,如果您使用的是表單身份驗證,則看不到您的呼叫

FormsAuthentication.SignOut();

將以下屬性添加到可在控制器中返回安全頁面的所有ActionResult方法中,都可以正常工作:

public class MyControllerForAuthorizedStuff
{
    [OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)]
    public ActionResult Index()
    {
        return View();
    }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM