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

后端 未结 10 2222
滥情空心
滥情空心 2020-12-01 00:21

I know there\'s a lot of questions on SO similar to this, but I couldn\'t find one for this particular issue.

A couple of points, first:

  • I have n
相关标签:
10条回答
  • 2020-12-01 00:35

    After a lot of trial and error, followed by a stagnant period while I waited for an opportunity to speak with our server guys, I finally had a chance to discuss the problem with them and asked them if they wouldn't mind switching our Sharepoint authentication over to Kerberos.

    To my surprise, they said this wouldn't be a problem and was in fact easy to do. They enabled Kerberos and I modified my app.config as follows:

    <security mode="Transport">
        <transport clientCredentialType="Windows" />
    </security>
    

    For reference, my full serviceModel entry in my app.config looks like this:

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="TestServerReference" closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="2000000" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
                 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="Windows" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://path/to/site/_vti_bin/Lists.asmx"
             binding="basicHttpBinding" bindingConfiguration="TestServerReference"
             contract="TestServerReference.ListsSoap" name="TestServerReference" />
        </client>
    </system.serviceModel>
    

    After this, everything worked like a charm. I can now (finally!) utilize Sharepoint Web Services. So, if anyone else out there can't get their Sharepoint Web Services to work with NTLM, see if you can convince the sysadmins to switch over to Kerberos.

    0 讨论(0)
  • 2020-12-01 00:39

    I have had this issue before.

    client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
    

    do this against your wcf proxy before making the call.

    0 讨论(0)
  • 2020-12-01 00:46

    I would try to connect to your Sharepoint site with this tool here. If that works you can be sure that the problem is in your code / configuration. That maybe does not solve your problem immediately but it rules out that there is something wrong with the server. Assuming that it does not work I would investigate the following:

    • Does your user really have enough rights on the site?
    • Is there a proxy that interferes? (Your configuration looks a bit like there is a proxy. Can you bypass it?)

    I think there is nothing wrong with using security mode Transport, but I am not so sure about the proxyCredentialType="Ntlm", maybe this should be set to None.

    0 讨论(0)
  • 2020-12-01 00:48

    Visual Studio 2005

    1. Create a new console application project in Visual Studio
    2. Add a "Web Reference" to the Lists.asmx web service.
      • Your URL will probably look like: http://servername/sites/SiteCollection/SubSite/_vti_bin/Lists.asmx
      • I named my web reference: ListsWebService
    3. Write the code in program.cs (I have an Issues list here)

    Here is the code.

    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());
                }
            }
        }
    }
    

    Visual Studio 2008

    1. Create a new console application project in Visual Studio
    2. Right click on References and Add Service Reference
    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. Make the following code changes:

    Change your app.config file from:

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

    To:

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

    Change your program.cs file and add the following code to your Main function:

    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();
    

    Add the using statements:

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

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

    0 讨论(0)
提交回复
热议问题