2013-02-07 104 views
1

我打了以下錯誤:請求,HTTP狀態401失敗:授權從C#asp.net訪問SharePoint Web服務

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> 

如果我能會丟失?

+0

看起來像您使用WCF服務一樣添加服務引用。您應該爲asmx服務添加「添加Web服務引用」。 – Flowerking

+0

@Flowerking,是的,我已經將它添加爲「添加Web服務引用」。 – Jack

回答

0

正如你只有「Windows身份驗證」服務器端啓用,嘗試改變,如下配置:

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

不,我改變配置,但仍然無法得到它的工作。 – Jack

+0

看着你的配置,我發誓你引用了列表服務兩次。一個是wcf引用,另一個是web引用?如果是這樣,擺脫wcf參考並重建解決方案再試一次。 – Flowerking

0

是您的C#ASP.NET同一物理服務器的SharePoint上運行?

如果是的話那麼它的一個很好的機會,其"Local Loopback Check"

相關問題