简体   繁体   English

当页面重新加载asp.net时,Cookie不起作用

[英]Cookies dont work when page gets reloaded asp.net

When my login button is clicked it should create this cookie 单击我的登录按钮后,应创建此cookie

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie testCookie =  new HttpCookie("UserInfo");
            testCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(testCookie);
        }
    }

    protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)
    {

    }

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {

    }

    protected void LoginView1_ViewChanged(object sender, EventArgs e)
    {

    }


    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        if (UserEmail.Text == "example@hotmail.com" && TextBox1.Text == "qwerty1")
        {

            HttpCookie cookie = Request.Cookies["UserInfo"];
            cookie["userName"] = UserEmail.Text;
            cookie["booleanCheck"] = "true";
            Response.Cookies.Add(cookie);
            Response.Redirect(Request.RawUrl);
        }
        else
        {
            ErrorMessage.Visible = true;
            ErrorEmail.Visible = true;
            ErrorPassword.Visible = true;
        }
    }
}

} }

Then the cookie that was saved above should be used to set the master page of the site. 然后,应使用上面保存的cookie来设置网站的母版页。

public partial class _Default : System.Web.UI.Page
    {
    Boolean found = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Page_PreInit(object sender, EventArgs e)
    {
        String signedInCheck = "false";
        HttpCookie cookie = Request.Cookies["userInfo"];
        if (cookie != null)
        {
            signedInCheck = cookie["booleanCheck"];

        }
        try
        {
            if (signedInCheck != "true")
                this.Page.MasterPageFile = "~/Site.Master";
            else
                this.Page.MasterPageFile = "~/LoggedIn.Master";

        }
        catch (Exception ex)
        {

        }
    }
}
}

This code works to switch to the second master page but when the page gets redrirected, even back to itself, it switches back to the first master page. 这段代码可以切换到第二个母版页,但是当页面重新定向甚至回到自身时,它也切换回第一个母版页。 How to change this so that when you are logged in you always display the second masterpage. 如何更改此设置,以便在登录时始终显示第二个母版页。

You need to add Days or expiration of Cookie 您需要添加Cookie的天数或有效期

HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);

Give it a try and see if it works for you. 尝试一下,看看它是否对您有用。

URL for you reference http://msdn.microsoft.com/en-us/library/ms178194.aspx 供您参考的URL http://msdn.microsoft.com/en-us/library/ms178194.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM