【问题标题】:The request failed with HTTP status 401: authorized for access sharepoint web service from C# asp.net请求失败,HTTP 状态 401:已授权从 C# asp.net 访问 sharepoint Web 服务
【发布时间】:2013-01-22 16:50:40
【问题描述】:

我遇到了以下错误:

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>

我会在哪里失踪?

【问题讨论】:

  • 看起来您添加服务引用就像使用 WCF 服务一样。您应该为 asmx 服务添加为“添加 Web 服务引用”。
  • @Flowerking,是的,我已经将它添加为“添加网络服务参考”。

标签: c# asp.net web-services sharepoint


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    如果是这样,那么它很有可能是"Local Loopback Check"

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 查看您的配置,我猜您引用了两次列表服务。一个作为 wcf 参考,一个作为 Web 参考?!如果是这样,请摆脱 wcf 引用并重建解决方案以重试。
      猜你喜欢
      • 2012-05-03
      • 2019-06-18
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多