Wednesday 15 February 2012

.net - why is use of "new NetworkCredential(username, password)" not working for Basic Authentication to my website (from a WinForms C# app)? -


I have a website that uses basic authentication (username / password).

Why is the following code not working? When I run it, the web application takes me to the login controller, while I hope that it should already be authenticated. I am populating credentials. In other words, I am trying to confirm that, in .NET, I Confirm my winforms HttpWebRequest so that it can automate the authentication process. I'm assuming that the network is the credential .net class that should do this? Or ntc What is the hope that some manual two step process do you have to implement yourself?

Here's the code:

  // Create a new web search for the specified URL. Var uri = new Uri ("http://10.1.1.102:3000/ download / escass"); Var myWebRequest = (HttpWebRequest) WebRequest.Create (Yuri); MyWebRequest.PreAuthenticate = true; Var NetworkCreative = New network credentials ("test", "asdfasdf"); MyWebRequest.Credentials = networkCredential; Var myWebResponse = (HttpWebResponse) myWebRequest.GetResponse (); Console.aut Type ("position =" + myWebResponse.StatusCode); Var stream = myWebResponse.GetResponseStream (); Var reader = new streamrider (stream); String text_read = reader.ReadToEnd (); Console.WriteLine (text_read); DisplayHtml (text_read); Reader.Close (); Stream.Close (); MyWebResponse.Close ();  

thanks

WebRequest Do not send credentials unless challenged by the site. The site should first respond with a "code-WWW-authentication header with 401 'authentication required'; Respond to the challenge with WebRequest credentials, and the service must respond with 200 HTP content.

It seems as if the site does not apply basic ath properly (imagine that first of all, the challenge should be passed in the field), which is a very common behavior you WebRequest Headers can integrate the original authentication from the collection, which most developers are doing anyway.

View


No comments:

Post a Comment