在我的母版页中我有注销。当我单击退出按钮时,将执行以下代码
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/