HTTP 请求未经客户端身份验证方案“Ntlm”的授权 从服务器收到的身份验证标头为“NTLM”

2024-04-30

我知道有很多与此类似的问题,但我找不到针对这一特定问题的问题。

首先有几点:

  • I have 无控制通过我们的 Sharepoint 服务器。我无法调整任何 IIS 设置。
  • 我相信我们的IIS服务器版本是IIS 7.0。
  • 我们的 Sharepoint Server 正在预测通过 NTLM 的请求。
  • 我们的 Sharepoint Server 与我的客户端计算机位于同一域中。
  • 我正在使用 .NET Framework 3.5、Visual Studio 2008

我正在尝试编写一个简单的控制台应用程序来使用 Sharepoint Web 服务操作 Sharepoint 数据。我已经添加了服务参考,以下是我的app.config:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="ListsSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://subdomain.companysite.com/subsite/_vti_bin/Lists.asmx"
            binding="basicHttpBinding" bindingConfiguration="ListsSoap"
            contract="ServiceReference1.ListsSoap" name="ListsSoap" />
    </client>
</system.serviceModel>

这是我的代码:

static void Main(string[] args)
{
    using (var client = new ListsSoapClient())
    {
        client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("username", "password", "domain");
        client.GetListCollection();
    }
}

当我调用 GetListCollection() 时,以下内容消息安全异常被抛出:

The HTTP request is unauthorized with client authentication scheme 'Ntlm'.
The authentication header received from the server was 'NTLM'.

使用内部 WebException:

"The remote server returned an error: (401) Unauthorized."

我尝试了各种绑定和各种代码调整来尝试正确进行身份验证,但无济于事。我将在下面列出这些。


我尝试了以下步骤:

在创建客户端之前使用本机 Win32 Impersonator

using (new Impersonator.Impersonator("username", "password", "domain"))
using (var client = new ListsSoapClient())
{
    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("dpincas", "password", "domain");
    client.GetListCollection();
}

这产生了相同的错误消息。


为我的客户端凭据设置 TokenImpersonationLevel

using (var client = new ListsSoapClient())
{
    client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
    client.GetListCollection();
}

这产生了相同的错误消息。


使用安全模式=TransportCredentialOnly

<security mode="TransportCredentialOnly">
    <transport clientCredentialType="Ntlm" />
</security>

这导致了不同的错误消息:

The provided URI scheme 'https' is invalid; expected 'http'.
Parameter name: via

但是,我需要使用 https,所以我无法更改我的 URI 方案。


我尝试过一些我不记得的其他组合,但我会在记住时发布它们。我实在是无计可施了。我在 Google 上看到很多链接都说“切换到 Kerberos”,但我的服务器似乎只接受 NTLM,而不是“协商”(如果它正在寻找 Kerberos,它会说),所以不幸的是,这不是一个选项。

各位有什么帮助吗?


视觉工作室2005

  1. 在 Visual Studio 中创建新的控制台应用程序项目
  2. Add a "Web Reference" to the Lists.asmx web service.
    • 您的网址可能如下所示:http://servername/sites/SiteCollection/SubSite/_vti_bin/Lists.asmx
    • 我将我的网络参考命名为:ListsWebService
  3. 在program.cs中编写代码(我这里有一个问题列表)

这是代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace WebServicesConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ListsWebService.Lists listsWebSvc = new WebServicesConsoleApp.ListsWebService.Lists();
                listsWebSvc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                listsWebSvc.Url = "http://servername/sites/SiteCollection/SubSite/_vti_bin/Lists.asmx";
                XmlNode node = listsWebSvc.GetList("Issues");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

视觉工作室2008

  1. 在 Visual Studio 中创建新的控制台应用程序项目
  2. 右键单击引用并添加服务引用
  3. Put in the URL to the Lists.asmx service on your server
    • Ex: http://servername/sites/SiteCollection/SubSite/_vti_bin/Lists.asmx
  4. Click Go
  5. Click OK
  6. 进行以下代码更改:

将您的 app.config 文件更改为:

<security mode="None">
    <transport clientCredentialType="None" proxyCredentialType="None"
        realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

To:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Ntlm"/>
</security>

更改您的program.cs文件并将以下代码添加到您的Main函数中:

ListsSoapClient client = new ListsSoapClient();
client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
XmlElement listCollection = client.GetListCollection();

添加 using 语句:

using [your app name].ServiceReference1;
using System.Xml;

参考:http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retriving-list-items-from-a-sharepoint-list http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTTP 请求未经客户端身份验证方案“Ntlm”的授权 从服务器收到的身份验证标头为“NTLM” 的相关文章

随机推荐