1

我遇到以下错误:

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.Config 文件

<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>

我会在哪里失踪?

4

3 回答 3

0

您的 C# ASP.NET 是否与 SharePoint 在同一物理服务器上运行?

如果是这样,那么它很有可能是“本地环回检查”

于 2013-02-07T19:03:46.303 回答
0

如果您的 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

于 2013-02-07T20:14:35.440 回答
0

由于您在服务器端仅启用了“Windows 身份验证”,请尝试更改配置,如下所示:

<security mode="TransportCredentialOnly">
        <transport clientCredentialType="Ntlm" proxyCredentialType="None"
                 realm="" />
于 2013-02-07T09:52:26.770 回答