Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
VB
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
[no name] 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.

You should probably use an HttpWebRequest and a cookie container:

C#
var client = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sUrl);
client.CookieContainer = new System.Net.CookieContainer();
 
Share this answer
 
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
On Page directive also use
EnableSessionState="False"

It works for me.
 
Share this answer
 
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)

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



Greetings from Portugal
Gandum
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900