Obtaining a cookie value from a logged in user on my website

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chopin
    New Member
    • Mar 2007
    • 37

    Obtaining a cookie value from a logged in user on my website

    I am trying to obtain the values of cookies from a particular website. The problem is, when I run the following code, it only loops through 4 of 10 cookie values present. Furthermore, the values are turning up as deleted: Reference

    Code:
     
    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://www.mysite.com");
    Request.CookieContainer = new CookieContainer();
    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
    
    foreach (Cookie cook in Response.Cookies)
           {                          
               Console.WriteLine("{0} = {1}", cook.Name, cook.Value);           
           }
    Then I came across this code, which states it will loop through every cookie value and obtain its values: Reference

    Code:
      
    int loop1, loop2;         
           HttpCookieCollection MyCookieColl;
           HttpCookie MyCookie;
           MyCookieColl = Request.Cookies;
    
           String[] arr1 = MyCookieColl.AllKeys; 
           for (loop1 = 0; loop1 < arr1.Length; loop1++)
           {
               MyCookie = MyCookieColl[arr1[loop1]];          
               String[] arr2 = MyCookie.Values.AllKeys;
           }
    The problem with the above, I can't seem to find a reference to "Request". I am using the libraries "using System.Net and
    using System.Web", so I am unsure why Request is not being recognized.

    My goal is to retrieve one cookie value so that when a user from my website logs in, my C# application will recognize that user.

    Thanks for any help!
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Try System.Web.Http Context.Current .Request

    Comment

    • chopin
      New Member
      • Mar 2007
      • 37

      #3
      I tried using this line here:

      Code:
      MyCookieColl = System.Web.HttpContext.Current.Request.Cookies;
      Unfortunately, upon executing that line of code, it prematurely opened my form, and terminated the program. I'm not sure how to proceed in obtaining the cookie values.

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        Quick question, is this an ASP.NET Web Application or a WinForms application?

        Comment

        • chopin
          New Member
          • Mar 2007
          • 37

          #5
          This is WinForms. I am developing an executable standalone program that will connect directly to my website's MySql database.

          Comment

          • chopin
            New Member
            • Mar 2007
            • 37

            #6
            I've done some more investigating, and it appears that this line of code is deleting my cookie values, which is the cause for my problem:

            Code:
            HttpWebResponse Response = HttpWebResponse)Request.GetResponse();
            I'm at a loss at what to do, it seems like the code in my first codeblock example is the correct way to accomplish what I need, the only problem being the above line deletes the values of the cookies I need.

            Comment

            • chopin
              New Member
              • Mar 2007
              • 37

              #7
              Maybe this will help clarify what I am trying to do. The equivalent code in PHP to obtain a cookie value is:

              Code:
              $_COOKIE['cookie_name']
              The above will output a cookie value if that cookie exists. That's it! Is there an equivalent function in C#?

              Comment

              • chopin
                New Member
                • Mar 2007
                • 37

                #8
                I've actually figured out how to do this by utilizing PHP instead. PHP has a built in function that can capture a cookie value by name, called $_COOKIE['cookie_name']. When a PHP page is loaded, I execute MySQL insert commands to input temp data into a table. C# will execute the PHP page executing the PHP commands which will insert the cookie value into a table when the C# program is loaded. Since C# can grab information from a MySQL database, this is a valid solution. Here is the code that will launch the script:

                Code:
                System.Diagnostics.Process.Start("www.example.com/script.php");
                Then C# will read the values from the temp table, storing the cookie values into a public static variable. My program will now recognize a username based on that cookie variable during the duration the program is opened.

                I don't know of any other way at the moment to capture a cookie value, at least natively in a C# WinForms application. But the above solution is 100% accurate and works well.

                Comment

                Working...