0
votes

I seem to be having a lot of issues reading a cookie from an old classic asp web application. we are slowly upgrading this web app to .net. The cookie holds some user information which also is used to tell the app that the person has authenticated successfully.

I try and read it like so:

Response.Cookies["mycookie"].Path = "/";
string strCookieText = Request.Cookies["mycookie"].Value;

Sometimes it seems to bring data back other times not but it is not consistent. I have also tried applying the same path when the cookie is created on the classic asp side but that seemed to really throw a wrench in the old app as then the classic asp side had a lot of challenges reading and finding the cookie.

So i figured i would create a function that would read in a classic asp page whos sole intent is to read the cookie. I don't like this method at all but I am out of ideas at this point. The issue here seems to be that it always comes back empty. I know there is data there however via fiddler and if i go to the actual site and hit the page. I am guessing this must be some pathing issue again perhaps or something like that that when i try and read it via .net it finds nothing.

here is my funciton that trys to read the page:

 public CCookie validateCookie()
    {
        CCookie ckCCookie = new CCookie();

        string strReadCookiePage = "";
        strReadCookiePage = GetHtmlPage("HTTP://MYPAGE/readcookie.asp");


        string[] strCarriageReturn = { "\n" };   //we are splitting on this character
        string[] strPageSplit;

        strPageSplit = strReadCookiePage.Split(strCarriageReturn, StringSplitOptions.RemoveEmptyEntries);

        string strCookieLength = "-2";

        foreach (string strValue in strPageSplit)
        {
            if (strCookieLength == "-1")
            {
                break;
            }

            if (strValue.Contains("<div id='arrayLength'>"))
            {
                strCookieLength = findStringValue(strValue, "<div id='arrayLength'>", "</div>");
            }
            if (strValue.Contains("<div id='VendorID'>"))
            {
                ckCCookie.UserVendorID = findStringValue(strValue, "<div id='VendorID'>", "</div>");
            }
            if (strValue.Contains("<div id='UserID'>"))
            {
                ckCCookie.UserID = Convert.ToInt64(findStringValue(strValue, "<div id='UserID'>", "</div>"));
            }
            if (strValue.Contains("<div id='LogonType'>"))
            {
                ckCCookie.LogonType = findStringValue(strValue, "<div id='LogonType'>", "</div>");
            }
            if (strValue.Contains("<div id='UserType'>"))
            {
                ckCCookie.UserType = findStringValue(strValue, "<div id='UserType'>", "</div>");
            }
        }

        //not able to validate the cookie
        if (strCookieLength == "-1")
        {

           //Response.Redirect("REDIRECT TO HAVE THEM LOG IN");
        }

        return ckCCookie;
    }

why isn't this working do you guys think. I need this to consistently work. I know i could save state to dbase but I don't want to do this this way as I think i should be able to read this thing.

any ideas or thoughts on how to make this work?

1

1 Answers

0
votes

Because of the way that cookies are stored they generally can't be shared between apps, like Classic ASP to ASP.NET.

I did find this article on MSDN that discusses solutions to the problem.

Personally, I would use a hidden field method to pass the cookie over, picking it up from the relevant control on the other side (ASP.NET).

-- EDIT --

Just so long as the hidden control resides in you form tags, you can try something like this (which is pretty much what ASP.NET does for it's __VIEWSTATE)...

<form id="myForm" method="post" action="myAspNetPage.aspx">
    <all_the_controls_i_need_on_my_form>
        ...
        ...
    </all_the_controls_i_need_on_my_form>
    <input id="cookieData" name="cookieData" type="hidden" value="<%= cookieData %>" />
</form>

The VBScript...

Dim cookieData
cookieData = Request.Cookie("MyCookie")

Obviously if you have several cookies that you want to pass in this way then you may need to concatenate them for splitting later...

Const C_SEPERATOR = "|"
Dim cookieData
cookieData = _
    "MyCookie1=" & Request.Cookie("MyCookie1") & C_SEPERATOR & _
    "MyCookie2=" & Request.Cookie("MyCookie2") & C_SEPERATOR & _
    "MyCookie3=" & Request.Cookie("MyCookie3") & C_SEPERATOR & _
    "MyCookie4=" & Request.Cookie("MyCookie4")

The above example could obviously be automated by introducing a For..Next loop, but this is simply an example. Note, also, that I have simply taken for granted that none of your cookies use the vertical bar character (C_SEPERATOR); if they do, then you'll have to find an alternative character.

On the other side, in your ASP.NET app you can the read the value from the post data and split it up...

Private Const C_SEPARATOR = "|"
...
...
Dim tCook As String = Request.Form("cookieData")
Dim cookeiData() As String
If tCook<>"" Then  cookieData = String.Split(tCook, C_SEPERATOR)

It's not the best solution - I'm sure that Lankymart will have something far better up his sleeve, but this will work.

PS: Apologies - I've just realised that I've done all this in VBScript. It should be pretty straightforward to convert to JScript, though.