简体   繁体   English

C#中的NetworkCredential在分配给HttpRequest对象时如何工作?

[英]How does NetworkCredential in C# works when assigned to a HttpRequest object?

I want to know how NetworkCredentials are passed over an http request. 我想知道NetworkCredentials是如何通过http请求传递的。 Do they get passed as HTTPHeaders, RequestData or there is something else that carries the information. 它们是作为HTTPHeaders,RequestData传递还是有其他东西携带信息。

I tried creating a sample app and checked the fiddler logs. 我尝试创建一个示例应用程序并检查了fiddler日志。 I don't see it being added as HTTPHeader so what carries that information. 我没有看到它被添加为HTTPHeader所以带有那些信息。

Here is the sample code I tried: 这是我尝试的示例代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://gmail.com");
request.Method = "GET";
request.ContentType = "application/json";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
request.Credentials = new NetworkCredential("TestUser", "Password-1");

From the following MSDN page; 从以下MSDN页面;

Supported authentication schemes include Digest, Negotiate, Kerberos, NTLM, and Basic. 支持的身份验证方案包括Digest,Negotiate,Kerberos,NTLM和Basic。 https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials(v=vs.110).aspx

So it will depend, usually I guess it will pick the most secure and available method... However it also says; 所以它将取决于,通常我猜它将选择最安全和可用的方法......但它也说;

To restrict HttpWebRequest to one or more authentication methods, use the CredentialCache class and bind your credentials to one or more authentication schemes 要将HttpWebRequest限制为一种或多种身份验证方法,请使用CredentialCache类并将您的凭据绑定到一个或多个身份验证方案

So if you wished it to be used as headers I suppose you could restrict it to only using Basic authentication using the CredentialCache object; 因此,如果您希望将其用作标题,我想您可以将其限制为仅使用CredentialCache对象进行基本身份验证;

https://msdn.microsoft.com/en-us/library/system.net.credentialcache(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.net.credentialcache(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM