I'm trying to download a file from a Sharepoint server, however my code to supply credentials. Its security is the sort that you get a little WinForms pop up asking for login details like that of a network drive as opposed to a logon screen.
This is what I am using
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("william.dunne", "legacy01");
client.DownloadFile(URL, Filename);
}
It seems you are missing the domain part of your username. Try supplying the domain to NetworkCredentials overloaded constructor.
Related
I have written a simple ASP.Net WebAPI that download a file from an on-prem tfs server using a rest call. The issue is when I connect to this site from my user account(corp domain), and set the credentials in webRequest to CredentialsCache.DefaultCredentials, it works fine since I am a vlid user on the tfs project.
Now someone else in my team wishes to use the same webAPI and when they hit the same url, I could see that its my credentials being used again to download the file.
I suspect this is because of using CredentialsCache.DefaultCredentials in my controller code, but I am not able to find a way using which I should be able to use the client's identity to be used for the download request.
My question is , is there a way to extract the windows identity or network credentials of the remote user(on the same domain) which I can use for Authentication. I am sorry if this is a DUP but I could not find a targetted answer to my question yet. Sample code is pasted below.
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
var webResponse = request.GetResponse();
var webStream = webResponse.GetResponseStream();
if (webStream != null)
{
var objReader = new StreamReader(webStream);
return objReader.ReadToEnd()
}
}
I dont know if I understand you correctly but I think impersonation is what you're looking for:
http://msdn.microsoft.com/en-us/library/xh507fc5.ASPX
I have a webpage which is windows forms authenticated ,and i want to download a copy of this page's HTML in to my server, when user request this page. I have tried something like this
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString("http://aksphases:200/lynliste.aspx");
}
which doesn't gives me correct result because of the URL I had passes to system creates new session.And in that case i need to authenticate this web-request,which I can't do.Only way to authenticate this webpage is that user log in manually(I know ways to authenticate werequests by code,but I can't try that here for some special reasons). Is there any other way for me to download current page's HTML which is running in in browser with out authenticating the URL.
You could send the current forms authentication cookie along with the request:
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.Cookie] =
System.Web.HttpContext.Current.Request.Headers["Cookie"];
string htmlCode = client.DownloadString("http://aksphases:200/lynliste.aspx");
}
This way we are basically transferring the current HTTP request cookies to the remote HTTP call.
If the web server does not allow anonymous access then there is no way around - you must authenticate yourself with the web site.
However, contrary to your belief that log on operation has to be done manually, it can be done via code also. In case of windows authentication, pass credentials via Credentials property. For Forms authentication, you need to POST log-on credentials to login page and then use the authentication cookie from the response in subsequent request (Use tool such fiddler to inspect request/responses from browser to replicate same within your code).
I am working on a problem right now and it involves downloading multiple PDF files to our DMZ from our LAN SharePoint server.
What we are trying to accomplish is a "NewsFeed" type application. What it does is take list data from uploaded Content Types made in SharePoint, then post them to our external website hosted on the DMZ, with a link to the appropriate pdf file. We used the REST API that sharepoint has to accomplish the text layout fairly easily, but the problem we are now running into is copying the .pdf of the uploaded document from the SharePoint site, onto the DMZ, so we can store them in a cache, and the user can access them through an href.
Here is code that works, to access the SharePoint REST API.
// Create the connection to sharepoint. Credentials are managed within web.config
SharePointListsREST.SharePointDataContext dc = new SharePointListsREST.SharePointDataContext(new Uri(ConfigurationManager.AppSettings["spUrl"]));
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTLM", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
dc.Credentials = cc;
This works perfectly, it is able to authenticate, grab the data and display it. For the PDF download functionality we have this.
private void GetPDF(string URL, string path)
{
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
WebClient wc = new WebClient();
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTML", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
wc.Credentials = cc;
wc.DownloadFile(URL, path);
}
which does not work at all and throws a 401 Not Authorized error.However, if you go to try to access the pdf file by going to http://XX.XX.XX.XX/.../test.pdf you will be prompted for a username/password. Entering my windows based credentials will give me access to the pdf.
I have tried using wc.UseDefaultCredentials = true; but it does not work either. Does anyone have any ideas on how I can get this to work with the webclient?
Here is one of the error logs from IIS.
2012-07-11 00:56:12 XX.XX.XX.XX GET /Employee+Images/_t/sample.jpg - 80 - 192.168.200.12 - 401 2 5 0
Impersonating the user and running whole WebClient code inside impersonated block is an option when you use Windows auth on SharePoint site.
See this question How can I use Directory.CreateDirectory with a different user id? on impersonating a user.
Or you simply can run whole process as that special user with Runas - no code changes would be needed.
I'm writing a google appengine application, which stores data and has a web front end. I want to be ableto pull down this data in a C# program. This means I need to authenticate with the site (users must be logged in to view the data). How can I authenticate like this? I tried setting credentials on the WebClient but I keep getting the google login page.
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
//should it be username#gmail.com ??
client.BaseAddress = "http://nosoperor-internal.appspot.com";
String s = client.DownloadString("/bank");
//s now contains the google login page, unfortunately
I found an answer in this blog:
Accessing authenticated Google App Engine services from a .NET CF client
and it works ^^
AFAIK, you need to probably use Google Data protocols and account APIs to authenticate.
Edit: I would download the .Net client library and try the examples in there. This is a copy paste from the docs:
Service service = new Service("cl", "exampleCo-exampleApp-1"));
// Set your credentials:
service.setUserCredentials("jo#gmail.com", "mypassword");
Similar to the CSV file which can be downloaded from http://download.finance.yahoo.com/d/quotes.csv?s=RHT . How can I downloada file which requires authentication?
I can simply use
My.Computer.Network.DownloadFile("http://download.finance.yahoo.com/d/quotes.csv?s=RHT,MSFT,NOVL&f=sl1c1d1&e=.csv", "h:\s.csv")
To download the file which is available in public. I tried setting the username and password as per the MSDN documentation but all I get is the HTML content of the login page.
If the site uses Cookie based authentication, you'll need to post the login details to the server, collect the cookies, then pass them up on the request for the file.
That's not as easy as it sounds...
There's an example here
http://blogs.msdn.com/dgorti/archive/2005/08/16/452347.aspx
Phil
It all depends on whether or not the web site will interpret your username/password when you provide it in your HTTP request. Some sites/protocols will, and others won't.
Here's an article that shows how you can download with the WebRequest class.
Downloading Files with the WebRequest and WebResponse Classes
All you need in addition to this article is adding your username/password to the WebRequest. You can do it like this:
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
string username = "your username";
string password = "your password";
request.Credentials = new System.Net.NetworkCredential(username, password);
...
}
Unfortunately, if using this method doesn't work, then Yahoo's finance page doesn't allow providing a username/password automatically, and you'd have to login "the old fashioned way" to be able to download your file.
It sounds like the site you are working with is using forms authentication. Unless you have access to a version that uses realm authentication you will probably need to use HttpWebRequest and fake POST request to the site while you have a CookieContainer so you can retain the token. Then you would be able to include that token in a get request to download the CSV file.