title: How to pass user name and password to Sharepoint web service?
body: <p>I am developing asp.net application for sharepoint 2010. I want to access sharepoint data in my asp.net application. Therefore I am using web service. The web service url starts with <code>https</code>. I am using the following code to access the web service.</p>
  
  <pre><code> 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&lt;PBSInternalContacts&gt; pbsInternalContactsList = query1.ToList();
              GridView1.DataSource = pbsInternalContactsList;
              GridView1.DataBind();
          }
  </code></pre>
  
  <p>My web.config file is as follows</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  
  &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="ApplicationServices"
           connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
           providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
  
      &lt;authentication mode="Windows"&gt;
        &lt;forms loginUrl="~/Account/Login.aspx" timeout="2880" /&gt;
      &lt;/authentication&gt;
  
      &lt;membership&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;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="/" /&gt;
        &lt;/providers&gt;
      &lt;/membership&gt;
  
      &lt;profile&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/&gt;
        &lt;/providers&gt;
      &lt;/profile&gt;
  
      &lt;roleManager enabled="false"&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /&gt;
          &lt;add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/roleManager&gt;
  
    &lt;/system.web&gt;
  
    &lt;system.webServer&gt;
       &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;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"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
            &lt;security mode="Transport"&gt;
              &lt;transport clientCredentialType="Ntlm" proxyCredentialType="None"
                realm="" /&gt;
              &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
          &lt;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"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
            &lt;security mode="Transport"&gt;
              &lt;transport clientCredentialType="Ntlm" proxyCredentialType="None"
                realm="" /&gt;
              &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
          &lt;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"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
            &lt;security mode="Transport"&gt;
              &lt;transport clientCredentialType="Ntlm" proxyCredentialType="None"
                realm="" /&gt;
              &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
        &lt;endpoint address="http://localhost/_vti_bin/Lists.asmx" binding="basicHttpBinding"
          bindingConfiguration="ListsSoap" contract="ListsServiceReference.ListsSoap"
          name="ListsSoap" /&gt;
        &lt;endpoint address="https://abc.info/_vti_bin/Lists.asmx"
          binding="basicHttpBinding" bindingConfiguration="ListsSoap1"
          contract="ServiceReference1.ListsSoap" name="ListsSoap1" /&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When I debug with the above code I am getting error 'The HTTP request is unauthorized with client authentication scheme 'Ntlm'. The authentication header received from the server was 'NTLM'.' I am getting error at the following line</p>
  
  <pre><code>XElement result = proxy.GetListItems("PBS Internal Contacts", null, query, viewFields, null, queryOptions, null);
  </code></pre>
  
  <p>How should I access the sharepoint web service with the username and password. Can you please provide me any code or link through which I can resolve the above issue ?</p>