Web Services 请求失败,HTTP状态为401:已授权从C#访问SharePoint Web服务asp.net

xoefb8l8  于 2022-11-15  发布在  C#
关注(0)|答案(3)|浏览(227)

我遇到以下错误:

The request failed with HTTP status 401: Unauthorized.

"法典"
代码如下:

protected void Execute_Click(object sender, EventArgs e)
    {


        /*Declare and initialize a variable for the Lists Web service.*/
        //Web_Reference.Lists myservice = new Web_Reference.Lists();
        ListsWebService.Lists myservice = new ListsWebService.Lists();

        /*Authenticate the current user by passing their default 
        credentials to the Web service from the system credential 
        cache. */
        myservice.Credentials =
           System.Net.CredentialCache.DefaultCredentials;

        /*Set the Url property of the service for the path to a subsite. Not setting this property will return the lists in the root Web site.*/
        //listService.Url = "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx";
        myservice.Url = "http://teamsites.ntu.edu.sg/sce/hrdev/_vti_bin/Lists.asmx";

        try
        {
            /*Declare an XmlNode object and initialize it with the XML 
            response from the GetListCollection method. */
            System.Xml.XmlNode node = myservice.GetListCollection();

            /*Loop through XML response and parse out the value of the
            Title attribute for each list. */
            foreach (System.Xml.XmlNode xmlnode in node)
            {
                Label1.Text += xmlnode.Attributes["Title"].Value + "\n";
            }
        }
        catch (Exception excep)
        {
            Label1.Text = excep.Message;
        }

    }

IIS设置-仅启用Windows身份验证。
Web配置文件

<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=bxxxxxxxx9" >
        <section name="ReadSharePointList.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=bxxxxxxxx9" requirePermission="false" />
    </sectionGroup>
</configSections>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
</system.web>

<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="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://mainsite/subsite/_vti_bin/lists.asmx"
            binding="basicHttpBinding" bindingConfiguration="ListsSoap"
            contract="StaffRole.ListsSoap" name="ListsSoap" />
    </client>
</system.serviceModel>
<applicationSettings>
    <ReadSharePointList.Properties.Settings>
        <setting name="ReadSharePointList_ListsWebService_Lists" serializeAs="String">
            <value>http://mainsite/subsite/_vti_bin/lists.asmx</value>
        </setting>
    </ReadSharePointList.Properties.Settings>
</applicationSettings>
</configuration>

我会错过什么呢?

huwehgph

huwehgph1#

由于您在服务器端只启用了“Windows身份验证”,请尝试按以下方式更改配置:

<security mode="TransportCredentialOnly">
        <transport clientCredentialType="Ntlm" proxyCredentialType="None"
                 realm="" />
ki1q1bka

ki1q1bka2#

您的C#ASP.NET是否与SharePoint运行在同一台物理服务器上?
如果是这样,那么它是一个很好的机会,它的"Local Loopback Check"

iezvtpos

iezvtpos3#

如果您的ASP.NET页位于同一个Intranet中,则可以使用System.Net.CredentialCache方法。请参见参考:http://msdn.microsoft.com/en-us/library/hh134614(v=office.14).aspx
如果您的ASP.NET页没有驻留在同一Intranet中,则需要通过Sharepoint站点的身份验证提供程序。请参阅参考:http://msdn.microsoft.com/en-us/library/hh147177(v=office.14).aspx

相关问题