Avatar of calypsoworld
calypsoworld
Flag for Portugal asked on

Interaction between Applet, Cookie e ASP.net doesn't works in Firefox

Hi!

I have an Applet that gets the username through a cookie set by asp.net page (the page that contains the applet).

Java Code to get the cookie
------------------------------

    private void getUserNameFromCookie() {
        try {
            // get the cookies that are applicable for this applet's parent web page
            URL docBaseUrl = this.getDocumentBase();

            CookieHandler cookieHandler = CookieHandler.getDefault();
            java.util.Map<String, List<String>> headers =
                    cookieHandler.get(docBaseUrl.toURI(),
                    new HashMap<String, List<String>>());

            if (headers.isEmpty()) {
                System.out.println("No cookies found!");
            } else {
                getUserNameFromHeader(headers);
            }
        } catch (Exception e) {
            System.out.println("Unable to get cookie using CookieHandler");
            e.printStackTrace();
        }
    }

    private void getUserNameFromHeader(java.util.Map<String, List<String>> headers) {

        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                if (key.equals("Cookie") && value.startsWith("userName")) {
                    userName = value.split("=")[1];
                }
            }
        }
    }

Open in new window


ASP.net C# code to create the cookie (on Page_Load)
----------------------

            HttpCookie appCookie = new HttpCookie("userName");
            appCookie.Value = Session["user"].ToString();

            appCookie.Expires = DateTime.Now.AddDays(1);
            appCookie.HttpOnly = false;
            Response.Cookies.Add(appCookie);

Open in new window


In IE everything works nice, but in Firefox the operations that requires the username from cookie are not launched.

Any idea on how to resolve this?

Thanks in advance!
ASP.NETJavaC#

Avatar of undefined
Last Comment
Daniel Junges

8/22/2022 - Mon
Daniel Junges

Daniel Junges

calypsoworld

ASKER
Thank you for reply.

Yes, cookies are enabled.
It's weird this works in IE and don't in Firefox....

Java console shows this information after applet's loading (and a lot of other things)

network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Apparently there are two cookies in this page (I don't know where the first cookie come from).
Considering that are two cookies in page, am I getting it in the right way in Java?

Thank you.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
calypsoworld

ASKER
I think that is the problem:

Information from Java console...

In IE
network: Connecting http://localhost:55000/page.aspx with cookie "userName=testuser@mail.com"

In Firefox
network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Maybe I'm getting the wrong index in Java...
How should script the Java code to get the value in userName field, both in IE and Firefox?
Thanks in advance!
ASKER CERTIFIED SOLUTION
calypsoworld

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Daniel Junges

very good.

regards junges