.NET 调用 SharePoint Web 服务收到 HTTP 401 未经授权的异常

发布于 2024-10-16 14:49:40 字数 1232 浏览 5 评论 0原文

我正在尝试调用 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-10-23 14:49:40

此代码是否在与 SharePoint 相同的服务器上本地运行 - 如果是这样,那么它可能是本地环回检查

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

另外 - 如果您尝试使用浏览器(以获取 WSDL)或 Web 服务测试工具(例如 SoapUI众多替代方案之一

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

把回忆走一遍 2024-10-23 14:49:40

我的网络应用程序是混合身份验证模式(声称 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; 

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文