2011-10-02 57 views
1

在我的母版页中,我有注销。当我点击登出按钮时,执行下面的代码注销必须在asp.net中点击两次

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

当我点击signout菜单消失,因为我做的网页基于角色的加载,所以它意味着角色权限被存储在会话中,因此它被清除,但页面必须重定向到Home.aspx,但它仍然在同一页面中,我必须再次单击注销才能重定向页面到home.aspx。我是否出错

+1

这是良好的调试进场的地方。浏览你的代码,看看第二个注销发生了什么,而不是第一个。 – 2011-10-02 00:45:11

回答

2

请尝试下面提到的代码。

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"); 
    } 
3

到退出操作可单独使用FormsAuthentication没有在你的代码触摸浏览器来促进,并且没有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. 

} 
+2

+1:不知道他为什么操纵cookies。 – TCM

相关问题