views:

959

answers:

1

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 or recommend a tutorial on how to do this with C#?

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.

+4  A: 

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();
Remus Rusanu
This is what you want, the accepted answer in that post is doing things the hard way but that may be required for what he's doing. Keep in mind that if you're using a proxy, you'll need to send separate credentials via request.Proxy.Credentials Also if you use a proxy, you may / may not be able to use PreAuthenticate. Also you don't necessarily have to do CredentialCache, you can just set request.Credentials = nc; Not sure what the difference is.
Allen
@Allen with Basic there is no difference between nc and cc. With Digest the cache does the right thing: only negotiates the digest once, then it pre-authenticates with a new increasing 'nc' (the digest nc, not the network cred in my example), thus avoiding the extra round trip. Therefore I believe is better to having the habit of using the cache rather than network creds.
Remus Rusanu