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

标签 c# asp.net authentication

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

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..
    }

当我单击注销时,菜单会消失,就像我在基于角色的页面加载中所做的那样,这意味着角色权限存储在 session 中,因此它被清除但页面必须重定向到 Home.aspx 但它仍然存在在同一页面中,我必须再次单击注销才能将页面重定向到 home.aspx。我哪里错了

最佳答案

仅使用 FormsAuthentication 即可促进注销操作无需触及代码中的 cookie,也无需 if 语句。 Forms Authentication 将自动为您处理 cookie 状态。

这也将解决双重注销问题,因为您在第一次单击注销按钮时将用户重定向到 protected 页面。

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. 

}

关于c# - 注销必须在 asp.net 中单击两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623887/

相关文章:

c# - 为什么我的表单不将参数传递给我的 Controller ?

http - 客户端应该如何将 facebook 访问 token 传递给服务器?

api - Symfony2 api 的身份验证(供移动应用程序使用)

c# - 如何使@Html.CheckBoxFor 不可见?

c# - 反射器中的列表

javascript - 使复选框成为一组单选按钮

c# - ASP.NET MVC 中类似 ListView 的实现

javascript - ASP.NET:如何使 onClientClick 在回发之前完成工作?

ASP.NET/MVC Bundler 缓存不起作用

linux -/bin/bash 版本太低,如何在登录时使用不同版本的 bash 获取点文件