Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# SSL Basic Access Authentication

I want to request reports from a third party and they require "Basic Access Authentication" via POST:

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 post but there are two answers and I'm not sure if thats what I need to do or which one is the preferred method.

like image 928
Chris Klepeis Avatar asked Feb 28 '23 13:02

Chris Klepeis


2 Answers

Assuming you use a WebRequest, you attach a CredentialCache to your request:

        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();
like image 119
Remus Rusanu Avatar answered Mar 06 '23 16:03

Remus Rusanu


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/

like image 20
Charlie Wu Avatar answered Mar 06 '23 15:03

Charlie Wu