无法使用标头中的Windows集成身份验证凭据创建httpwebrequest [英] Cannot create httpwebrequest with windows integrated authentication credentials in headers

查看:87
本文介绍了无法使用标头中的Windows集成身份验证凭据创建httpwebrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的C#desktop
程序中使用Windows集成身份验证从HTTP服务器获取一些网页。我不希望用户再次输入用户名,密码或域名。这是我用来尝试测试获取HTTP GET中信息的代码的代码:


  //准备请求 

HttpWebRequest webrequest =

(HttpWebRequest)WebRequest.Create(" http://test.local/wia" );

webrequest.Method = WebRequestMethods.Http.Get;

webrequest.Accept = " text / plain" ;

webrequest.KeepAlive = false ;

webrequest.AllowAutoRedirect = true ;

webrequest.AllowWriteStreamBuffering = true ;

webrequest.KeepAlive = true ;

webrequest.ContentLength = 0;

webrequest.ProtocolVersion = new 版本(" 1.1" ) ;



CredentialCache cred = new CredentialCache();

cred.Add(" http://test.local.wia" ,8020,"NTLM"

CredentialCache.DefaultNetworkCredentials);

webrequest.Credentials = cred;



//显示请求

System.Diagnostics.Debug.WriteLine ("请求信息开始");

System.Diagnostics.Debug.WriteLine(" URI =" +

webrequest.RequestUri) ;

System.Diagnostics.Debug.WriteLine(" Method =" +

webrequest.Method) ;

System.Diagnostics.Debug.WriteLine(" Request Info END" );

System.Diagnostics.Debug.WriteLine(" Request Headers START" );

System.Diagnostics.Debug.WriteLine(webrequest.Headers);

System.Diagnostics.Debug.WriteLine(" Request Headers END" );



解决方案

除非服务器请求,否则不会发送身份验证它。所以你看到它是在以后的请求中发送的吗?参见例如 http://blogs.msdn.com/feroze_daud/archive/2004/07/12/ 180856.aspx

Hi,
I am trying to use Windows Integrated Authentication in my C# desktop
program to get some web pages from a HTTP server.  I don't want the user to
have to type in their username, password or domain again.  This is the code I
have been using to try and test getting the credentials into the HTTP GET
message:

           // prepare the request

            HttpWebRequest webrequest = 

(HttpWebRequest)WebRequest.Create("http://test.local/wia");

            webrequest.Method = WebRequestMethods.Http.Get;

            webrequest.Accept = "text/plain";

            webrequest.KeepAlive = false;

            webrequest.AllowAutoRedirect = true;

            webrequest.AllowWriteStreamBuffering = true;

            webrequest.KeepAlive = true;

            webrequest.ContentLength = 0;

            webrequest.ProtocolVersion = new Version("1.1");



            CredentialCache cred = new CredentialCache();

            cred.Add("http://test.local.wia", 8020, "NTLM", 

CredentialCache.DefaultNetworkCredentials);

            webrequest.Credentials = cred;



            // display the request

            System.Diagnostics.Debug.WriteLine("Request Info START");

            System.Diagnostics.Debug.WriteLine("URI = " + 

webrequest.RequestUri);

            System.Diagnostics.Debug.WriteLine("Method = " + 

webrequest.Method);

            System.Diagnostics.Debug.WriteLine("Request Info END");

            System.Diagnostics.Debug.WriteLine("Request Headers START");

            System.Diagnostics.Debug.WriteLine(webrequest.Headers);

            System.Diagnostics.Debug.WriteLine("Request Headers END");



解决方案

Authentication won't be sent unless the server requests it.  So do you see it being sent on later requests?  See e.g. http://blogs.msdn.com/feroze_daud/archive/2004/07/12/180856.aspx


这篇关于无法使用标头中的Windows集成身份验证凭据创建httpwebrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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