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.
Related
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.
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 want to download a ssrs report in Excel format in code.
So i first checked the url: it is correct
then i created a webrequest, with a NetworkCredential for the same user which can open the link in the browser.
I moved developing to the ssrs machine itself ( it has vs2008 on it)
so now i'm on the ssrs machine, logged on as user A, starting a webpage, create a webrequest with the same credentials as i'm currently logged on with to the ssrs machine (user A).... and get a 401.
What i don't know is what is giving me the 401. Is it the webserver denying me, or is it ssrs itself (i'm a newbee at ssrs so i don't know much about rights on reports in ssrs itself, but as i said: my logon credentials are the same as the webrequest credentials: user A).
i've googled a lot and tried 5 solutions, but no luck so far.
This won't work if NTLM credentials are required:
webrequest.Credential = new networkcredential ("","","");
You can try the following but it likely will not work:
webrequest.Credentials = CredentialCache.DefaultNetworkCredentials;
Chances are you will need to pass in the actual credentials like:
NetworkCredential networkCredential = new NetworkCredential(UserName, Password, Domain);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(url), "NTLM", networkCredential);
webrequest.Credentials = credCache;
SSRS will need to authentic the WebRequest and default/blank credentials cannot be passed in.
This reminded me of something I ran into a couple years ago:
http://support.microsoft.com/kb/896861
I'm not sure how likely this problem is, but I really think it's worth implementing "Method 2" from that support article (a simple registry addition) just to see if that resolves the problem. (Yes, the article lists a bunch of old OS versions, but I successfully used this on a bunch of SharePoint 2007 servers to resolve problems.)
Worth a try, I think...
I always set
WebRequest.UseDefaultCredentials = True
Then I run the app as the user accessing the SSRS server.
At first I suggest installing Fiddler Web Debugger on the machine where the calling application is run. This way you can see exactly the requests and responses that are made between your application and the SSRS. You might find something like an ISA proxy wanting you to additionally authenticate to it.
Assuming your caller is a console or WebForms application, the following code passes the credentials under which your application runs to the SSRS:
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
// request authentication
httpWebRequest.UseDefaultCredentials = true;
// which is the same as
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
// optional proxy authentication
httpWebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
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.