为什么使用"新的NetworkCredential(用户名,密码)QUOT;不工作的基本身份验证到我的网站(从一个WinForms C#应用程序)? [英] why is use of "new NetworkCredential(username, password)" not working for Basic Authentication to my website (from a WinForms C# app)?

查看:407
本文介绍了为什么使用"新的NetworkCredential(用户名,密码)QUOT;不工作的基本身份验证到我的网站(从一个WinForms C#应用程序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用基本身份验证(用户名/密码)的网站。

I have a website that uses Basic Authentication (username/password).

为什么以下code不工作?当我运行的Web应用程序需要我登录控制器,而我期待它应该已经被认证给我填充的凭据。换句话说我想确认如何在.NET中,我确认我的WinForms HttpWebRequest的,这样它会自动完成身份验证过程。我assumeing说的NetworkCredential是应该这样做的.NET类?或者在.NET中有一个期望有一些手工两步过程中,你必须实现自己吗?

Why is the following code not working? When I run it the web application takes me to the login controller, whereas I'm expecting that it should already be authenticated given I'm populating the credentials. In other words I'm trying to confirm how, in .NET, I confirm my winforms HttpWebRequest so that it will automate the authentication process. I'm assumeing that NetworkCredential is the .net class that should do this? Or in .NET is there an expectation there is some manual two step process you have to implement yourself?

下面是code:

    // Create a new webrequest to the mentioned URL.            
    var uri = new Uri("http://10.1.1.102:3000/download/sunset");
    var myWebRequest = (HttpWebRequest)WebRequest.Create(uri);            
    myWebRequest.PreAuthenticate=true;            
    var networkCredential=new NetworkCredential("test", "asdfasdf");                        
    myWebRequest.Credentials=networkCredential;
    var myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

    Console.Out.WriteLine("STATUS = " + myWebResponse.StatusCode);

    var stream = myWebResponse.GetResponseStream();
    var reader = new StreamReader(stream);
    string text_read = reader.ReadToEnd();
    Console.WriteLine(text_read);
    DisplayHtml(text_read);
    reader.Close();
    stream.Close();
    myWebResponse.Close();

感谢

推荐答案

WebRequest的不发证书,除非质疑的网站。该网站应该首先做出反应401需要授权与WWW身份验证头中,WebRequest的将响应凭据面临的挑战和服务应与200 HTTP内容作出回应。

WebRequest does not send credential unless challenged by the site. The site should respond first 401 'Authorization Required' with an WWW-Authenticate header, the WebRequest will respond to the challenge with credentials and the service should respond with the 200 http content.

喜欢的网站Shounds没有实现基本认证正确(规格说它shoudl第一个挑战,通过在领域中),这是一个很常见的行为。您可以手动添加的基本身份验证的WebReauest.Headers colelction,这是大多数开发者到底是什么反正做什么。

Shounds like the site does not implement Basic auth properly (the spec says it shoudl challenge first, to pass in the realm) which is a very common behavior. You can add the Basic authentication manually to the WebReauest.Headers colelction, which is what most developers end doing anyway.

请参阅<一href="http://stackoverflow.com/questions/1702426/httpwebrequest-not-passing-credentials/1702529#1702529">http://stackoverflow.com/questions/1702426/httpwebrequest-not-passing-credentials/1702529#1702529

这篇关于为什么使用&QUOT;新的NetworkCredential(用户名,密码)QUOT;不工作的基本身份验证到我的网站(从一个WinForms C#应用程序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆