Cookies getting lost

时间:2016-10-20 18:59:27

标签: asp.net iis cookies

I am transferring data from one aspx web page to another using cookies. It is working perfectly in the local (local host) machine IIS 7.5 but not on IIS 8.0 server after publishing

Response.Cookies["UserBID"].Value = "11111";
        Response.Cookies["AppID"].Value = "161";
        Response.Cookies["RoleID"].Value = "6";
        Response.Cookies["ActiveOnly"].Value = "1";
        Response.Cookies["UserBID"].Expires = DateTime.Now.AddMinutes(5);
        Response.Cookies["AppID"].Expires = DateTime.Now.AddMinutes(5);
        Response.Cookies["RoleID"].Expires = DateTime.Now.AddMinutes(5);
        Response.Cookies["ActiveOnly"].Expires = DateTime.Now.AddMinutes(5);
        //Response.Redirect("http://localhost:49752/Usermgmt.aspx");
        Response.Redirect("http://dnadev/GlobalUserManagement/Usermgmt.aspx",false);

getting back

if (Request.Cookies["UserBID"] != null)
        {
            UserBID = Request.Cookies["UserBID"].Value.ToString();
        }
        if (Request.Cookies["AppID"] != null)
        {
            AppID = Request.Cookies["AppID"].Value.ToString();
        }
        if (Request.Cookies["RoleID"] != null)
        {
            RoleID = Request.Cookies["RoleID"].Value.ToString();
        }
        if (Request.Cookies["ActiveOnly"] != null)
        {
            ActiveOnly = Request.Cookies["ActiveOnly"].Value.ToString();
        }

Thanks,

1 个答案:

答案 0 :(得分:0)

From what code you've given I cant really tell what the problem is. I will attempt to answer your question by walking through the process. When you set a cookie in the response you should use Response.SetCookie(MyCookie); This will not make duplicate cookies. When just setting the value of cookies it can potentially create duplicates. After you set the cookie you will have to reload the page or visit another one for the cookie to be set. It will not show up in the Request.Cookies["MyCookie"]; Other than that I don't see any problems with your code.

Set Cookie:

HttpCookie cookie = new HttpCookie("UserBid", "11197");
HttpContext.Response.SetCookie(cookie);

Get Cookie Value:

var bid = (Request.Cookies["UserBid"] != null) ? Request.Cookies["UserBid"].Value : "";
相关问题