C# 必须在asp.net中单击两次注销

C# 必须在asp.net中单击两次注销,c#,asp.net,login,C#,Asp.net,Login,在我的母版页中,我有注销。当我单击注销按钮时,执行以下代码 protected void singout_Click(object sender, EventArgs e) { Session.Abandon(); if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { HttpCookie myCookie = new HttpCook

在我的母版页中,我有注销。当我单击注销按钮时,执行以下代码

protected void singout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
        {
            HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
            FormsAuthentication.SignOut();
            Response.Redirect("Home.aspx");
        }
    }
在同一母版页中,我的负载是

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
       Menu Items..
    }

当我单击“注销”时,菜单会消失,因为我在基于角色的页面加载中这样做,因此这意味着角色权限存储在会话中,因此它会被清除,但页面必须重定向到Home.aspx,但它仍保留在同一页面中,我必须再次单击“注销”,页面才能重定向到Home.aspx。哪里出了问题

请尝试下面提到的代码

protected void singout_Click(object sender, EventArgs e) 
    { 
        Session.Abandon(); 
        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
        { 
            HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName); 
            myCookie.Expires = DateTime.Now.AddDays(-1d); 
            Response.Cookies.Add(myCookie); 
            FormsAuthentication.SignOut(); 
        }
        Response.Redirect("Home.aspx"); 
    } 

请尝试下面提到的代码

protected void singout_Click(object sender, EventArgs e) 
    { 
        Session.Abandon(); 
        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
        { 
            HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName); 
            myCookie.Expires = DateTime.Now.AddDays(-1d); 
            Response.Cookies.Add(myCookie); 
            FormsAuthentication.SignOut(); 
        }
        Response.Redirect("Home.aspx"); 
    } 

只需使用即可方便注销操作,无需触摸代码中的cookie,也无需使用
if
语句。表单身份验证将自动为您处理cookie状态

这也将解决双重注销的问题,因为在第一次单击注销按钮时,您将用户重定向到远离受保护页面的位置

protected void singout_Click(object sender, EventArgs e)
{
    Session.Abandon();
    //Removes the forms-authentication ticket from the browser:
    FormsAuthentication.SignOut(); 

    FormsAuthentication.RedirectToLoginPage();
    // ...or redirect the user to any place of choice outside the protected files. 

}

只需使用即可方便注销操作,无需触摸代码中的cookie,也无需使用
if
语句。表单身份验证将自动为您处理cookie状态

这也将解决双重注销的问题,因为在第一次单击注销按钮时,您将用户重定向到远离受保护页面的位置

protected void singout_Click(object sender, EventArgs e)
{
    Session.Abandon();
    //Removes the forms-authentication ticket from the browser:
    FormsAuthentication.SignOut(); 

    FormsAuthentication.RedirectToLoginPage();
    // ...or redirect the user to any place of choice outside the protected files. 

}

这就是良好的调试发挥作用的地方。仔细检查代码,看看第二次注销与第一次注销相比发生了什么。这就是良好的调试发挥作用的地方。逐步浏览代码,查看第二次注销与第一次注销相比发生了什么。