Click here to Skip to main content
14,125,060 members
Rate this:
 
Please Sign up or sign in to vote.
Hey guys, I have a console application which I'm trying to connect to my website and send back an authentication cookie. If I paste the url directly in to the browser the cookie is returned just fine but when I try connect through the console application I get the following error "Too many automatic redirections were attempted." when I hit the client.OpenRead(sUrl)

Any help is very much apreciated

Console Application
string sUrl = "http://localhost/CreateCookie.aspx?UserName=" + username + "&Password=" + password;

           client.Headers.Add("user-agent",
               "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

           using (Stream data = client.OpenRead(sUrl))
           {
               using (StreamReader reader = new StreamReader(data))
               {
                   strCookie = reader.ReadToEnd();
               }
           }


Web Page
Dim sUserName As String = Request.QueryString("UserName")
Dim sPassword As String = Request.QueryString("Password")

Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, sUserName, DateTime.Now, DateTime.Now.AddDays(30), False, _
"SignalR", FormsAuthentication.FormsCookiePath)
'/ Encrypt the ticket.
Dim encTicket As String = FormsAuthentication.Encrypt(ticket)
' // Create the cookie.
Response.Write(encTicket)
Response.End()
Posted
Updated 4-Jul-18 23:35pm
Comments
InsertCleverUserName 31-Mar-14 20:15pm
   
http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-http
frostcox 1-Apr-14 3:40am
   
Thank you so much my error has stopped. Really appreciate the help.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You should probably use an HttpWebRequest and a cookie container:

var client = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sUrl);
client.CookieContainer = new System.Net.CookieContainer();
   
Comments
frostcox 1-Apr-14 3:40am
   
Thank you so much my error has stopped. Really appreciate the help.
Chamadness 2-Apr-14 8:24am
   
Welcome, glad to help
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

On Page directive also use
EnableSessionState="False"

It works for me.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

hi,

i had this error but got a simple fix
you don't need all that code, all you need is to do is in the beginning of your application download the cookie like this (sorry but i work with VB :) but is pretty simple to convert)

[your application namespace].Application.GetCookie(New Uri("https://[site]"))



Greetings from Portugal
Gandum
   
Comments
Richard Deeming 5-Jul-18 10:35am
   
FOUR YEARS too late, and with code that only works in a WPF application, not a console application as stated in the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 5 Jul 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100