Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass credentials to httpwebrequest for accessing SharePoint Library

I'm trying to read files from a SharePoint document library using HttpWebRequest. In order to do that I have to pass some credentials. I'm using the below request:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "application/msexcel"; request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0"; request.Credentials = new NetworkCredential(UserName, PassWord); 

Is this the correct way to pass credentials?

like image 295
Tortoise Avatar asked Mar 04 '11 05:03

Tortoise


2 Answers

You could also use:

request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;  
like image 133
Mihai Brustur Avatar answered Oct 12 '22 05:10

Mihai Brustur


If you need to run request as the current user from desktop application use CredentialCache.DefaultCredentials (see on MSDN).

Your code looks fine if you need to run a request from server side code or under a different user.

Please note that you should be careful when storing passwords - consider using the SecureString version of the constructor.

like image 42
Alexei Levenkov Avatar answered Oct 12 '22 06:10

Alexei Levenkov