0 Replies - 6449 Views - Last Post: 19 March 2013 - 09:54 AM

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3163
  • View blog
  • Posts: 9,559
  • Joined: 05-May 12

Authenticates with .NET 4.5, but not .NET 3.5 - what's happening?

Posted 19 March 2013 - 09:54 AM

I'm trying to get the following code working on .NET 3.5. It works correctly when the code is compiled to target .NET 4.5, but changing the target to .NET 3.5 returns the following WebException on line 28:

Quote

System.Net.WebException was unhandled
Message=Too many automatic redirections were attempted.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at GetDrupal35.Program.Run() in c:\z\GetDrupal35\GetDrupal35\Program.cs:line 28
at GetDrupal35.Program.Main(String[] args) in c:\z\GetDrupal35\GetDrupal35\Program.cs:line 42
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


With Fiddler2 running, it's pretty obvious that with .NET 3.5, the exception is correct with regards to the redirects:

Quote

GET http://drupalserver/user/login/sso
401 Authorization Required (text/html)

GET http://drupalserver/user/login/sso
302 Found to http://drupalserver/

GET http://drupalserver/
401 Authorization Required (text/html)

GET http://drupalserver/
302 Found to http://drupalserver/...so?destination=

GET http://drupalserver/...so?destination=
401 Authorization Required (text/html)

GET http://drupalserver/...so?destination=
302 Found to http://drupalserver/


The last 4 GET's just keep cycling over and over again.

Anybody know what changed in .NET 4.5's HttpWebRequest that allows code that obviously doesn't work in .NET 3.5?

Here's the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace GetDrupal35
{
    class Program
    {
        const string baseUri = "http://drupalserver";
        const string ssoUri = "http://drupalserver/user/login/sso";
        const string reqUri = "http://drupalserver/services/solr_service/search.xml?keys=boston&ic=10&page=0";

        void Run()
        {
            var container = new CookieContainer();

            CredentialCache cache = new CredentialCache();
            NetworkCredential networkCreds = new NetworkCredential("username", "password");
            cache.Add(new Uri(baseUri), "BASIC", networkCreds);

            var request = (HttpWebRequest)WebRequest.Create(ssoUri);
            request.Credentials = cache;
            request.CookieContainer = container;

            var response = request.GetResponse();
            var text = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(text);

            request = (HttpWebRequest)WebRequest.Create(reqUri);
            request.Credentials = cache;
            request.CookieContainer = container;

            response = request.GetResponse();
            text = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(text);
        }

        static void Main(string[] args)
        {
            new Program().Run();
        }
    }
}


This post has been edited by Skydiver: 19 March 2013 - 10:04 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1