Hugo Estrada
Hugo Estrada

Reputation: 627

Silverlight HttpWebRequest Fails to include .ASPXAUTH Cookie in Header

Right now I have a silverlight app that gets data from a mvc site. As long as I use WebClient and to GET requests, everything seems to be okay. However, when I use HttpWebRequest and POST, then I am getting authentication problems.

The problems stem from the request lacking the .ASPXAUTH cookie.

Does anyone know how to solve or work around this?

Upvotes: 4

Views: 1726

Answers (2)

Heretic Monkey
Heretic Monkey

Reputation: 12114

I recently got bit by this too. You're hitting the separation of brower-based communication and Silverlight-based communication. When you use WebClient, you're actually making the request through the browser's HTTP stack, and therefore get all of the automatic cookie handling. When you use HttpWebRequest, you're using Silverlight's HTTP stack, and therefore cannot use the cookies sent to the browser; the two cookie stores are separate. See this article for more details.

I was writing out @Zain's answer, which registers the HttpWebRequest with the browser's HTTP stack and works around the issue, but he beat me to it ;-).

Upvotes: 1

Zain Shaikh
Zain Shaikh

Reputation: 6043

write following code before making HttpWebRequest object.

System.Net.WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp); System.Net.WebRequest.RegisterPrefix("https://", WebRequestCreator.BrowserHttp);

and then create the HttpWebRequest object.

// Create request
HttpWebRequest request = HttpWebRequest.CreateHttp("http://zainshaikh.posterous.com/");

Hope this helps.

Upvotes: 6

Related Questions