C# SSL 基本访问认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1127223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# SSL Basic Access Authentication
提问by Chris Klepeis
I want to request reports from a third party and they require "Basic Access Authentication" via POST:
我想从第三方请求报告,他们需要通过 POST 进行“基本访问身份验证”:
Your client application must use Basic Access Authentication to send the user name and password.
Your client application must use Basic Access Authentication to send the user name and password.
Can someone point me in the right direction?
有人可以指出我正确的方向吗?
Edit: I did see this postbut there are two answers and I'm not sure if thats what I need to do or which one is the preferred method.
编辑:我确实看到了这篇文章,但有两个答案,我不确定这是否是我需要做的,或者哪个是首选方法。
采纳答案by Remus Rusanu
Assuming you use a WebRequest, you attach a CredentialCache to your request:
假设您使用 WebRequest,您将 CredentialCache 附加到您的请求:
NetworkCredential nc = new NetworkCredential("user", "password");
CredentialCache cc = new CredentialCache();
cc.Add("www.site.com", 443, "Basic", nc);
WebRequest request = WebRequest.Create("https://www.site.com");
request.Credentials = cc;
request.PreAuthenticate = true;
request.Method = "POST";
// fill in other request properties here, like content
WebResponse respose = request.GetResponse();
回答by Charlie Wu
The basic gist is like this:
基本要点是这样的:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Credentials = new NetworkCredential(username, password);
but sometimes there are issues with using request credentials, the alternative is add the authentication data in request headers
但有时使用请求凭据会出现问题,另一种方法是在请求标头中添加身份验证数据
string authInfo = username + ":" + password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
for more details see this blog post
有关更多详细信息,请参阅此博客文章
http://charlie.cu.cc/2012/05/how-use-basic-http-authentication-c-web-request/
http://charlie.cu.cc/2012/05/how-use-basic-http-authentication-c-web-request/