views:

462

answers:

1

I'm trying to figure out how to use the System.Net.WebRequest class to send my credentials when the Apache server has a .htaccess file protecting access. It appears to be something to do with the Credentials property (imagine that!) but I am not sure how to create the correct ICredentials object. Sample code = accepted answer.

+3  A: 
System.Net.CredentialCache aCredentialCache = new System.Net.CredentialCache();
aCredentialCache.Add(new Uri("www.yoursite.com"), "Basic", new System.Net.NetworkCredential("User", "Password"));

System.Net.WebRequest aRequest = System.Net.WebRequest.Create("www.yoursite.com");
aRequest.Credentials = aCredentialCache;
Rick Mogstad
Thanks! And welcome to the site
Luke