Getting cookies of previous page

  • Thread starter Thread starter Zeba
  • Start date Start date
Z

Zeba

Hi !

I want to know the cookie value that was set in the previous page.

Initially in my page in project B I used

HttpCookie cookie = Request.Cookies.Get(somefile.COOKIE_NAME);

where cookie name is set using web.config. As a result when I call
this page of project B from some page in Project A, I get the cookie
of project B. But I want to get the cookie in the Request.Cookie from
the page in Project A. How do i do that ?

Thanks!
 
Zeba said:
Initially in my page in project B I used

HttpCookie cookie = Request.Cookies.Get(somefile.COOKIE_NAME);

where cookie name is set using web.config. As a result when I call
this page of project B from some page in Project A, I get the cookie
of project B. But I want to get the cookie in the Request.Cookie from
the page in Project A. How do i do that ?

Request.Cookies["CookieName"] will allways return to you the cookie
regardless of the page that set it, on the condition that the browser is
sending to you that cookie in the Request. The browser will only send
cookies when the request is going to the same domain (such as
www.mydomain.com) that originally sent that cookie in its Response. So, as
long projectA and projectB both have a URL below the same domain, you
shouldn't have any problem getting the cookie.
 
Actually, i retrieve the cookie name from
myUXE.Lib.Web.AppConfigCookie.COOKIE_NAME which is actually provided
to me as .dll files to include in the project. This .dll file is
available in both the projects A and B.

Project A sets its cookie name in its web.config and Project B in its
web.config. So when in Project B's page I give
HttpCookie cookie =
Request.Cookies.Get(myUXE.Lib.Web.AppConfigCookie.COOKIE_NAME);
it actually accesses the cookie with the cookie name set by Project B.
How do I get the cookie name set by Project A ?

Thanks !
 
Zeba said:
Actually, i retrieve the cookie name from
myUXE.Lib.Web.AppConfigCookie.COOKIE_NAME which is actually provided
to me as .dll files to include in the project. This .dll file is
available in both the projects A and B.

Project A sets its cookie name in its web.config and Project B in its
web.config. So when in Project B's page I give
HttpCookie cookie =
Request.Cookies.Get(myUXE.Lib.Web.AppConfigCookie.COOKIE_NAME);
it actually accesses the cookie with the cookie name set by Project B.
How do I get the cookie name set by Project A ?

I misunderstood the original question. I thought that the problem was
recovering the cookie itself, when your problem is actually finding out the
name of the cookie.
If I have understood what you are doing, you are calling a DLL that
retrieves the name from its .config file. You want to force the DLL that is
in your project to read the .config file from another project. This is not
going to be easy if you are not allowed to modify the source code of the
DLL. The config file is set when the AppDomain that loads the DLL is
created, and in the case of a web app this is done by aspnet_wp and you
can't change its behaviour as far as I know.
I can think of some alternatives: One is to create an AppDomain, set the
config file to the config in the other project, and load the DLL inside it,
then call the dll.
Another alternative would be to create a folder below the main folder of
projectB, create a second web.config in that folder, copy into it the
relevant portion of the web.config in projectA, and run the page that needs
the cookie name in that folder. The web.config in the subfolder should take
priority over the web.config in the main folder.
A third alternative would be to write a webservice in projectA to return
the cookie name, and call that webservice from projectB.
 
Thanks a lot ! That was interesting. I was thinking if I could just
parse the referrer url and check the project name and thus set my
cookie name ( hard-coding ) for the time being.

But when I use something like
if (Request.UrlReferrer.ToString().Contains("http://localhost/
mySite"))
I get the error that UrlReferrer object is not instantiated.
How do I get the url of the calling page?

Thanks !
 
Zeba said:
if (Request.UrlReferrer.ToString().Contains("http://localhost/
mySite"))
I get the error that UrlReferrer object is not instantiated.
How do I get the url of the calling page?

UrlReferrer is not reliable. It only works if the browser is sending the
"Referrer" HTTP header, which is optional, so you can't depend on all
browsers sending it.
But anyway, if the browser does send it, Request.UrlReferrer should be
instantiated and should provide you with the value that you want. Where are
you executing that code? If you do it in Page_Load it should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top