0

Alright, so. For some reason, this cookie is getting deleted/overwritten.
It does exists for awhile. well, for one cycle of Code 2, but when code 2 is called again VIA a page load, it doesnt contain my data.
What the cookie contains is a encoded session ID which is used to determine who that user is and if they are an Admin or not.
and if it doesnt have the sessionID to use to compare, it directs the user to the login page.

Assume the User Role is set to 1.

Code 1: http://pastebin.com/rfP7se0x
Code 2: http://pastebin.com/c2Jy7zQ1 (stripped allot of code not pertaining to cookies.)
Code 3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FormsAuthenticateProject.Customer
{
    public partial class Customer : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DBConnLayer DBCL = new DBConnLayer();
                DBCL.userRoleRedirect();
            }
        }
    }
}
  • do you have a time or date for how long the cookie should be active and or deleted..? take a look here https://msdn.microsoft.com/en-us/library/system.net.cookie.expires(VS.80).aspx – MethodMan Mar 04 '15 at 22:24
  • @MethodMan I believe I did that in code 1, line 48 to 56 – HorribleScripter Mar 05 '15 at 17:20

2 Answers2

0

Within the userRoleRedirect (http://pastebin.com/c2Jy7zQ1) you are making the following conditional check:

else if (ckDateTime.CompareTo(DateTime.Now) > 0)

You need to check if this will ever evaluate to true. I suspect that the cookie expiry time means that the difference between Now and the cookie expiry time does not add up to greater than 0. The culprit bug could lie in the following code which parses the expiry time:

 DateTime.TryParseExact(myCookie.Values["LogoutTimer"], DTFormat,
                CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out ckDateTime);
gimg1
  • 1,121
  • 10
  • 24
  • That code does evaluate to true on the first run. But on the 2nd run the value is null. the cookie is new and the date it spits out is 01/jan/01. The expiry is also set to that. – HorribleScripter Mar 05 '15 at 17:16
0

This answer has already been answered here: cookie values disappear when traversing between content pages

I added this code into the first line of userRoleRedirect

if (Request.Cookies["Session"] != null)
{
    Response.Cookies.Set(Request.Cookies["Session"]);
}

and HttpCookie myCookie = Request.Cookies["Session"];
to var myCookie = Request.Cookies["Session"].Values;
I fetch values using
myCookie["ValueName"]
It returns a string.

Community
  • 1
  • 1