如何将用户名和密码传递给Sharepoint Web服务?

时间:2012-08-21 05:42:48

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

我正在为sharepoint 2010开发asp.net应用程序。我想在我的asp.net应用程序中访问sharepoint数据。因此我使用的是Web服务。 Web服务网址以https开头。我使用以下代码访问Web服务。

 private void TryOutCode()
        {
            //SharepointWebApplication.ListsServiceReference.ListsSoap s;
            SharepointWebApplication.ServiceReference1.ListsSoapClient proxy = new SharepointWebApplication.ServiceReference1.ListsSoapClient();

            proxy.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
            //proxy.ClientCredentials.Windows.ClientCredential.UserName = @"anil-pc\owner";
            //proxy.ClientCredentials.Windows.ClientCredential.Password = "123";
            //proxy.ClientCredentials.Windows.AllowNtlm = true;
            //proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential("owner", "123", "anil-pc");
            proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

            //Microsoft.SharePoint.SPListItem li = new Microsoft.SharePoint.SPListItem();

            //Define the parameters for the service call
            XElement query = new XElement("Query");
            XElement queryOptions = new XElement("QueryOptions");
            XElement viewFields = new XElement("ViewFields");

            XElement result = proxy.GetListItems("PBS Internal Contacts", null, query, viewFields, null, queryOptions, null);

            //XDocument list_data = XDocument.Parse(result);

            XNamespace ns = "#RowsetSchema";

            var query1 = from x in result.Descendants()
                         where x.Name == ns + "row"
                         select new PBSInternalContacts
                         {
                             User = x.Attribute("ows_User").Value,
                             Role = x.Attribute("ows_Role").Value,
                         };

            List<PBSInternalContacts> pbsInternalContactsList = query1.ToList();
            GridView1.DataSource = pbsInternalContactsList;
            GridView1.DataBind();
        }

我的web.config文件如下

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Windows">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <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="Transport">
            <transport clientCredentialType="Ntlm" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="ListsSoap1" 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="Transport">
            <transport clientCredentialType="Ntlm" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="ListsSoap2" 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="Transport">
            <transport clientCredentialType="Ntlm" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/_vti_bin/Lists.asmx" binding="basicHttpBinding"
        bindingConfiguration="ListsSoap" contract="ListsServiceReference.ListsSoap"
        name="ListsSoap" />
      <endpoint address="https://abc.info/_vti_bin/Lists.asmx"
        binding="basicHttpBinding" bindingConfiguration="ListsSoap1"
        contract="ServiceReference1.ListsSoap" name="ListsSoap1" />
    </client>
  </system.serviceModel>
</configuration>

当我使用上面的代码调试时,我收到错误'HTTP请求未经授权使用客户端身份验证方案'Ntlm'。从服务器收到的身份验证标头是“NTLM”。我在下一行收到错误

XElement result = proxy.GetListItems("PBS Internal Contacts", null, query, viewFields, null, queryOptions, null);

如何使用用户名和密码访问sharepoint Web服务。能否请您提供我可以解决上述问题的任何代码或链接?

1 个答案:

答案 0 :(得分:2)

我已经在C#中传递了凭据,如下所示。

proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential("owner", "123", "anil-pc");

Web.config文件具有以下设置

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

现在一切正常。我可以访问sharepoint列表数据。