Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET calling SharePoint Web Service gets an HTTP 401 Unauthorized exception

I am trying to call a SharePoint Lists service to get list definition and data. The SharePoint site is my companies but I have no control over it. Here is all I know about the server's security:

Server is HTTPS:// Server accepts Windows Active Directory credentials when logging in...

I have tried Basic, Digest, CredentialCache, just NetworkCredential, UnsafeAuthenticatedConnectionSharing, UseDefaultCredentials, PreAuthenticate... not sure what the proper config is...

The error I receive is HTTP 401 Unauthorized.

                Uri url = new Uri(baseAddress + "/_vti_bin/Lists.asmx", UriKind.Absolute);
                Lists.Lists client = new Lists.Lists();

                // sometimes works
                CredentialCache cache = new CredentialCache();
                cache.Add(url, "NTLM", new NetworkCredential(context.UserName, context.Password, context.Domain));
                client.UseDefaultCredentials = false;
                client.Credentials = CredentialCache.DefaultCredentials;
                // doesn't work ever
                //client.Credentials = new NetworkCredential(context.UserName, context.Password, context.Domain);
                //client.PreAuthenticate = true;
                client.UnsafeAuthenticatedConnectionSharing = true;
                client.Url = url.AbsoluteUri;
                listData = client.GetList(listName).OuterXml;
like image 610
markti Avatar asked Nov 06 '22 03:11

markti


2 Answers

My web application is a mixed authentication mode (claims NTLM + FBA), below code works for me.

HttpWebRequest req = WebRequest.CreateHttp("https://example.com/");
req.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); //login with windows account
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
req.Credentials = new NetworkCredential(“username", “password", "domain”); 
//this also works
//req.Credentials = CredentialCache.DefaultNetworkCredentials; 
like image 43
Wint Avatar answered Nov 09 '22 15:11

Wint


Is this code running locally on the same server as SharePoint - if so then its probably the local loopback check.

If not then you need to give some acurate details - is your site using Integrated Authentication (and if so is it via NTLM or Kerberos) or Basic or Forms?

Also - what happens if you try to use that web service using a browser (to get the WSDL) or a web service test tool such as SoapUI or one of the many alternatives

like image 168
Ryan Avatar answered Nov 09 '22 15:11

Ryan