开发者

Sign out has to be clicked twice in asp.net

In my master page I have the signout. When I click the sign out button the below code is executed

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();
            Respon开发者_如何学编程se.Redirect("Home.aspx");
        }
    }

and in the same master page my load is

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

When I click the signout the menu disappears as I do it in the page load based on role, so it means the role permission is stored in session so it gets cleared but the page has to redirect to Home.aspx but it remains in the same page, I have to click the sign out again for the page to redirect to home.aspx. WHere am I going wrong


A logout operation can be facilitated solely by using FormsAuthentication without touching the cookie in your code, and without the if statement. Forms Authentication will automatically take care of the cookie state for you.

This will also solve the problem of double sign-out because you are redirecting the user away from the protected page the first time the sign-out button is clicked.

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. 

}


Try the under mentioned code.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜