Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass user credentials to web service?

I am consuming a webservice using WSDL in windows application. When I try to use method, i get the following error:-

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was '"

{"The remote server returned an error: (401) Unauthorized."}

I have user credentials but don't know how to pass it using c# code in windows application.

like image 406
user1327064 Avatar asked Jun 10 '13 20:06

user1327064


1 Answers

Here is the how it is working for me:-

Config file setting looks like this:-

<configuration>
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="bindingName"  >
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" proxyCredentialType="None" realm=""/>
                <message clientCredentialType="UserName" algorithmSuite="Default"/>
              </security>
            </binding>

          </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://10.10.10.10:1880/testpad/services/Testwebservice"
                binding="basicHttpBinding" bindingConfiguration="bindingName"
                contract=testService.GetData" name="test_Port1" />
        </client>
    </system.serviceModel>
</configuration>

and here i am passing user credentials:-

 var ser = new GetDataClient();
 ser.ClientCredentials.UserName.UserName = "userid";
 ser.ClientCredentials.UserName.Password = "Pa$$word1";
like image 154
user1327064 Avatar answered Oct 16 '22 09:10

user1327064