.NET调用SharePoint Web服务获取HTTP 401 Unauthorized异常

时间:2011-02-10 04:12:07

标签: .net web-services sharepoint active-directory ntlm

我正在尝试调用SharePoint列表服务来获取列表定义和数据。 SharePoint网站是我的公司,但我无法控制它。以下是我对服务器安全性的了解:

服务器是HTTPS:// 登录时,服务器接受Windows Active Directory凭据...

我尝试过Basic,Digest,CredentialCache,只是NetworkCredential,UnsafeAuthenticatedConnectionSharing,UseDefaultCredentials,PreAuthenticate ......不确定正确的配置是什么......

我收到的错误是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;

2 个答案:

答案 0 :(得分:1)

此代码是否在与SharePoint相同的服务器上本地运行 - 如果是,则可能是local loopback check

如果没有,那么您需要提供一些精确的详细信息 - 您的网站是使用集成身份验证(如果是通过NTLM或Kerberos)还是基本或表单?

此外 - 如果您尝试使用浏览器(获取WSDL)或Web服务测试工具(例如SoapUIone of the many alternatives

)使用该Web服务会发生什么?

答案 1 :(得分:1)

我的网络应用程序是混合身份验证模式(声称NTLM + FBA),下面的代码适用于我。

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;