Failed to upload file.

I am trying to download a file from a C # application. I tried two different methods, but both give the same answer: "The remote server responded with an error: (401)" Unauthorized ".

I am sure this is an account (because of 401). If I go to the URL from the browser and enter the same credentials, the file will load just fine. In "Attempt 2" (below), for type authtype, I tried: NTLM, Basic, Negotiate and Digest with no luck.

Does anyone see what I can do wrong here?

Thanks for the help!

Attempt 1:

string username = "username"; string password = "password"; string domain = "domain"; string url = @"http://LiveLinkInstance.com/livelink/llisapi.dll/999999/WordDocument.docx?func=doc.Fetch&nodeid=999999&ReadOnly=True&VerNum=-2&nexturl=/livelink/llisapi.dll?func=ll&objId=888888&objAction=browse&viewType=1"; // Create an instance of WebClient WebClient client = new WebClient(); client.Proxy = null; client.Credentials = new System.Net.NetworkCredential(username, password, domain); client.DownloadFile(new Uri(url), @"C:\FileDownloads\test.txt"); 

Attempt 2:

 string username = "username"; string password = "password"; string domain = "domain"; string url = @"http://LiveLinkInstance.com/livelink/llisapi.dll/999999/WordDocument.docx?func=doc.Fetch&nodeid=999999&ReadOnly=True&VerNum=-2&nexturl=/livelink/llisapi.dll?func=ll&objId=888888&objAction=browse&viewType=1"; HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(domain + "\\" + username + ":" + password)); wr.Headers.Add("Authorization", "Basic " + credentials); CredentialCache cc = new CredentialCache(); cc.Add(new Uri(url), "NTLM", new NetworkCredential(username, password, domain)); wr.Credentials = cc; Stream str = ws.GetResponseStream(); 
+4
source share
3 answers

As Amitai said, using a violinist to compare with browser traffic is the best way to go. BTW, look at SO here - what happens in the OP case was that the request was redirected to another location, but the credentials were not retransmitted. In this way, the OP performed a manual redirection to solve the problem.

+1
source

You tried

 client.UseDefaultCredentials = true 

if you use MVC or WebApi, you should decorate your method

 [Authorize] 

If you can impersonate a user, use him like this:

  WindowsIdentity wi = null; wi = (WindowsIdentity)HttpContext.Current.User.Identity; using (wi.Impersonate()) { var client = new WebClient { UseDefaultCredentials = true }; client.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8"); var result = JsonConvert.DeserializeObject<Object>(Encoding.UTF8.GetString(client.DownloadData("http://api.com/api/values"))); return Request.CreateResponse(result); } 
+1
source

I have seen LL using my own form-based authentication or IWA-based SSO. I do not know if you can use other types of HTTP authentication.

If your server uses the (default) authentication form, you will have to use LAPI or WS to load the document providing the LL credentials in the LAPI / WS call. You can also just get a cookie for HTTP communication using LAPI / WS.

If you configured SSO, you can set credentials in CredentialCache.DefaultCredentials to pass credentials to an authenticated Windows session.

0
source

All Articles