问题描述
我试图让在.NET中使用WebRequest类网页的请求。我想读的URL要求Windows身份验证,由于我得到一个未经授权的异常。我如何传递一个Windows凭据这一要求,以便它可以进行身份验证。
I am trying to make a request to a web page using WebRequest class in .net. The url that I am trying to read requires Windows Authentication due to which I get an unauthorised exception. How can I pass a windows credentials to this request so that it can authenticate.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( "http://myapp/home.aspx" );
request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential( "username", "password", "domain" );
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Raises Unauthorized Exception
this.Response.Write( response.StatusCode );
以上code返回以下错误。
The above code returns the following error.
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
我注意到一件事,同时检查异常的详细信息是,我尝试访问的URL会重新导向至一个不同的URL这是促使我提供NT登录详细信息。我认为,证书应被转发到这个网址为好。但显然情况并非如此。
I noticed one thing while checking the exception details is that the url that I am trying to access is redirecting to a different url which is prompting me to provide the NT login details. I believe that the credentials should get forwarded to this url as well. But apparently it is not happening.
推荐答案
我试图访问一个链接路过的Windows凭据。链路A然后重定向我自动链路B,但没有通过,我曾提供的Windows凭据。因此错误。我做request.AutoRedirect =假,并通过每次我的位置在头即我手动每次路过Windows凭据做我的重定向时间循环。
I am trying to access a link A passing the windows credentials. Link A then redirects me to link B automatically but does not pass the windows credentials which I had supplied. Hence the error. I did request.AutoRedirect = false, and looped through every time I get location in the header i.e. I do my redirects manually each time passing the windows credentials.
这为我工作:)
这篇关于制作一个网页请求一个网页,要求Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!