使用 HttpWebRequest 进行网络身份验证和网站身份验证

发布于 2024-10-10 02:13:01 字数 701 浏览 1 评论 0原文

我正在尝试创建一个使用 .NET Framework 使用 RSS 数据的应用程序。 RSS 站点需要用户名和密码才能启动。 我在我的工作场所运行此应用程序,需要 NTLM 身份验证才能连接到互联网。

以下是我尝试使用的代码,

NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = cache;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

我收到 407 错误,如果我只是使用 CredentialCache.DefaultNetworkCredentials,我收到 401 错误。

I am trying to create a application that will consume RSS data using .NET Framework. The RSS site requires User name and Password to start with.
and Am running this application from within my work place which requires NTLM authentication to connect to internet.

Following is the code that i am trying to use

NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = cache;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

i get 407 error and if i simply use CredentialCache.DefaultNetworkCredentials i get 401 error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-10-17 02:13:01

使用以下代码解决了该问题

 string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password"));
 StringBuilder outputData = new StringBuilder();
 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
 myHttpWebRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
 myHttpWebRequest.Headers.Add("Authorization", "Basic " + credentials);
 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
 Stream streamResponse = myHttpWebResponse.GetResponseStream();

solved the issue by using following code

 string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password"));
 StringBuilder outputData = new StringBuilder();
 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
 myHttpWebRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
 myHttpWebRequest.Headers.Add("Authorization", "Basic " + credentials);
 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
 Stream streamResponse = myHttpWebResponse.GetResponseStream();
迷离° 2024-10-17 02:13:01

如果这段代码有效,那么上面的原始代码是错误的。您应该设置

request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Credentials = cache;

换句话说,您已经交换了代理和目标服务器的凭据。

If this code works, then your original code above was wrong. You should set

request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

and

NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Credentials = cache;

In other words, you had exchanged credentials for the proxy and the destination server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文