System.Net.NetworkCredential的第一个参数

本文讲述在.Net应用程序中,使用WebRequest请求网页,通过代理服务器访问时出现407错误。经反复检查用户帐号和密码无误后仍无法通过验证,最终发现NetworkCredential构造函数第一个参数只认用户名,修改代码后执行成功。

System.Net.NetworkCredential是FCL中一个密码身份验证的提供凭据类。今天我在一个.Net应用程序中,使用WebRequest请求一个网页,由于是在局域网当中,必须通过代理服务器材能够访问,设置了WebProxy和密码凭据后,访问网页出现407错误。其代码大致如下:

WebProxy _wpr = new WebProxy("http://isaserver:2000",true) ;
NetworkCredential _cred = new NetworkCredential(@"CropDomain/UserName","Password");
_wrp.Credentials = _cred;
WebRequest _wrq = WebRequest.CreateDefault(new Uri("page url"));
_wrq.Proxy = _wrp;
WebResponse _wrp = _wrq.GetResponse(); 

反复跟踪,检查了用户帐号、密码,正确无误,但是就是无法通过代理的验证。后来把第2句代码改成了:
NetworkCredential _cred = new NetworkCredential("UserName","Password","CropDomain");
KO!代码执行成功。

本来是想偷个懒的把用户帐号直接写在NetworkCredential构造函数的第一个参数里,没有想到这个参数其实只认用户名,而不会对包含域名的用户帐号做解析,害得我调试了一个上午。

TlsException: Handshake failed - error code: UNITYTLS_INTERNAL_ERROR, verify result: UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED Mono.Unity.Debug.CheckAndThrow (Mono.Unity.UnityTls+unitytls_errorstate errorState, Mono.Unity.UnityTls+unitytls_x509verify_result verifyResult, System.String context, Mono.Security.Interface.AlertDescription defaultAlert) (at <34cafa714676469f8b8b2caaccc23f12>:0) Mono.Unity.UnityTlsContext.ProcessHandshake () (at <34cafa714676469f8b8b2caaccc23f12>:0) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) (at <34cafa714676469f8b8b2caaccc23f12>:0) (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool) Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) (at <34cafa714676469f8b8b2caaccc23f12>:0) Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) (at <34cafa714676469f8b8b2caaccc23f12>:0) Rethrow as AuthenticationException: Authentication failed, see inner exception. Mono.Net.Security.MobileAuthenticatedStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) (at <34cafa714676469f8b8b2caaccc23f12>:0) (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.AuthenticateAsClient(string,System.Security.Cryptography.X509Certificates.X509CertificateCollection,System.Security.Authentication.SslProtocols,bool) System.Net.Security.SslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.TlsStream.AuthenticateAsClient () (at <34cafa714676469f8b8b2caaccc23f12>:0) (wrapper remoting-invoke-with-check) System.Net.TlsStream.AuthenticateAsClient() System.Net.FtpControlStream.PipelineCallback (System.Net.CommandStream+PipelineEntry entry, System.Net.ResponseDescription response, System.Boolean timeout, System.IO.Stream& stream) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.CommandStream.PostReadCommandProcessing (System.IO.Stream& stream) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.CommandStream.PostSendCommandProcessing (System.IO.Stream& stream) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.CommandStream.ContinueCommandPipeline () (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.CommandStream.SubmitRequest (System.Net.WebRequest request, System.Boolean isAsync, System.Boolean readInitalResponseOnConnect) (at <34cafa714676469f8b8b2caaccc23f12>:0) (wrapper remoting-invoke-with-check) System.Net.CommandStream.SubmitRequest(System.Net.WebRequest,bool,bool) System.Net.FtpWebRequest.TimedSubmitRequestHelper (System.Boolean isAsync) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.FtpWebRequest.SubmitRequest (System.Boolean isAsync) (at <34cafa714676469f8b8b2caaccc23f12>:0) --- End of stack trace from previous location where exception was thrown --- System.Net.FtpWebRequest.CheckError () (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.FtpWebRequest.SyncRequestCallback (System.Object obj) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.FtpWebRequest.RequestCallback (System.Object obj) (at <34cafa714676469f8b8b2caaccc23f12>:0) (wrapper remoting-invoke-with-check) System.Net.FtpWebRequest.RequestCallback(object) System.Net.CommandStream.InvokeRequestCallback (System.Object obj) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.CommandStream.Abort (System.Exception e) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.FtpWebRequest.FinishRequestStage (System.Net.FtpWebRequest+RequestStage stage) (at <34cafa714676469f8b8b2caaccc23f12>:0) System.Net.FtpWebRequest.GetRequestStream () (at <34cafa714676469f8b8b2caaccc23f12>:0) UpLoad.Ftpupload (System.String filePath, System.String fileName) (at Assets/Editor/UpLoad.cs:42) UpLoad.uploadallfiles () (at Assets/Editor/UpLoad.cs:25)
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值