ASP.NET身份验证登录和使用浏览器后退按钮注销

时间:2010-04-21 22:18:38

标签: asp.net asp.net-membership back-button

我正在寻找一个解决方案,供用户使用浏览器的后退按钮,一旦注销就导航到上一页。

我在asp.net中构建了一个Web应用程序,并使用自定义成员资格提供程序进行身份验证和授权。一切正常,除非用户点击退出链接退出应用程序并重定向到默认封面页,如果用户点击浏览器上的BACK BUTTON,它实际上会回到原来的状态。数据仍将显示。

当然,他们无法在该页面上做任何事情,点击任何链接,他们将再次重定向到登录页面。但是显示这些信息会让很多用户感到困惑。

我只是想知道是否有任何方法可以清除浏览器的历史记录,因此无法返回,或者当他们点击后退按钮并让他们重定向到登录页面时。

感谢

7 个答案:

答案 0 :(得分:23)

担心浏览器历史记录和后退按钮会让您头痛和生殖器疣。内置了一些设施来解决这个问题。

您的退出链接/按钮应指向包含此代码的页面以及您想要的任何其他内容。

[vb.net]

Imports System.Web.Security

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles MyBase.Load
    Session.Abandon()
    FormsAuthentication.SignOut()
End Sub

并[c#]

using System.Web.Security;

private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here
    Session.Abandon();
    FormsAuthentication.SignOut();
}

代码来自此page并且有效,但页面很难看。

可以找到关于后退按钮行为的好问题/答案here

<强>更新

根据我与Matthew的对话,禁用敏感或动态的单个页面上的缓存可以通过以下代码完成:

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

我很想知道它是否适合你,就像它对我一样。

答案 1 :(得分:3)

您可以使用javascript来禁用后退按钮(通常是将用户发送到转发到另一个页面的页面,以便点击后退再向前发送)。持久用户仍然可以在历史记录中退回2步并跨越循环。

该页面位于浏览器的缓存中。您可以要求浏览器不缓存任何内容,但这会破坏性能,有时会非常显着,所以我不推荐它。

答案 2 :(得分:1)

此代码非常有用

Response.Cache.SetCacheability(HttpCacheability.NoCache);

仅将此代码放在主页面上的加载事件中以防万一,但它仅适用于IE,适用于我使用的IE和Firefox

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

答案 3 :(得分:1)

Your Answer

解决方法是将以下javascript代码添加到logout.aspx页面的部分:

<script type="text/javascript">
 window.history.forward(1);
</script>

如果用户通过按后退按钮进入退出页面,此javascript代码将转发用户。

如果您需要确保用户在注销后无法返回页面,则必须要求浏览器不要通过在每个页面上包含类似以下内容的代码来缓存任何页面:

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
Response.Cache.SetNoStore(); 

答案 4 :(得分:0)

如果有帮助,您可以尝试使用HttpResponse.Cache属性:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;

if (Response.Cache.VaryByParams["Category"])
{
//…
}

或者可以使用HttpResponse.CacheControl完全阻止页面的缓存,但是它已经被弃用而支持上面的Cache属性:

Response.CacheControl = “No-Cache”;

或者你真的可以疯了,手工完成:

Response.ClearHeaders();
Response.AppendHeader(“Cache-Control”, “no-cache”); //HTTP 1.1
Response.AppendHeader(“Cache-Control”, “private”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “no-store”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “must-revalidate”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “max-stale=0″); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “post-check=0″); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “pre-check=0″); // HTTP 1.1
Response.AppendHeader(“Pragma”, “no-cache”); // HTTP 1.1
Response.AppendHeader(“Keep-Alive”, “timeout=3, max=993″); // HTTP 1.1
Response.AppendHeader(“Expires”, “Mon, 26 Jul 1997 05:00:00 GMT”); // HTTP 1.1

Reference

答案 5 :(得分:0)

最好的解决方法是在母版页中放置以下代码。这样可以避免缓存页面,并防止用户注销后对其进行访问。

P.S:以下代码是来自各种来源的编译。将其张贴在这里,以便任何寻求解决方案的人都可以找到有用的

Master.cs

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore(); 

    }

Master.aspx

<a href="logout.aspx">Logout</span></a>

logout.cs

protected void Timer1_Tick(object sender, EventArgs e)
        {
            Session.Clear();
            Session.Abandon();


 Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();

        try
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1000;
            Response.CacheControl = "no-cache";
            //Response.Redirect("login.aspx", true);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        Response.Redirect("Signin.aspx");
    }

logout.aspx

<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Text="Loggin Out Please Wait" runat="server" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
            </asp:Timer>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

</div>
</form>

答案 6 :(得分:-3)

实际上我找到了一个解决方案,我将以下代码段添加到母版页的页面加载方法中。

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

感谢您的回复:)