Results 1 to 2 of 2

Thread: consuming a webservice that requires login credentials

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    consuming a webservice that requires login credentials

    I am trying to consume a webservice that requires a username and password to connect. I have a working example in Java of how to do this, but I am trying to write the equivalent solution in C#.

    Here is the Java code:
    Code:
    import myplace.MY3004527.webservice.MY3004527WebServiceBindingStub; 
    import myplace.MY3004527.webservice.MY3004527WebServiceServiceLocator; 
    
    public class MyManager { 
    
       MY3004527WebServiceBindingStub myWS; 
       public MyManager() { super(); } 
    
       public void initialize() throws ServiceException { 
    
           MY3004527WebServiceServiceLocator service = new MY3004527WebServiceServiceLocator(); 
           
           myWS = new MY3004527WebServiceBindingStub( 
                 new URL("https://myservice.someplace.com/MY3004527/MY3004527WebService"), 
                 service); 
           myWS.setUsername("theuser"); 
           myWS.setPassword("secretpassword"); 
    } 
    
    . 
    . 
    .
    I added a web reference in my Visual Studio C# Console application by entering the address to the wsdl. I had to enter the username and password for it to be able to see it and complete generation of the web service code.

    (Note I have used SoapUI to verify the webservice is working correctly and after entering username password there it worked fine. Also, works with Java code.)

    Here's what I have so far in my non-working C# code version:
    Code:
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.IO; 
    using System.Collections; 
    using MyManagerConsumer.MyManagerWS; 
    using System.ServiceModel; 
    
    namespace MyManagerConsumer 
    { 
        class Program 
        { 
            MyManagerWS.MY3004527WebService myWS; 
            
            public void initialize() 
            { 
                //System.ServiceModel.Channels.Binding binding = new System.ServiceModel.Channels.Binding(); 
                //ADPX1001635WebService service = new IOManagerWS.ADPX1001635WebServiceClient(binding, remoteAddress); 
                //System.ServiceModel.Channels.Binding binding = new System.ServiceModel.Channels.Binding 
                System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport); 
                System.ServiceModel.EndpointAddress remoteAddress = new System.ServiceModel.EndpointAddress("https://myservice.someplace.com/MY3004527/MY3004527WebService"); 
    
                //System.ServiceModel.Description.ClientCredentials loginCredentials= new System.ServiceModel.Description.ClientCredentials(); 
                //loginCredentials.UserName.UserName = "theuser"; 
                //loginCredentials.UserName.Password = "secretpassword"; 
                
                System.ServiceModel.Description.ContractDescription contract = new System.ServiceModel.Description.ContractDescription("contractName"); 
                System.ServiceModel.ChannelFactory factory = contract.Behaviors.Find<ChannelFactory>(); 
                factory.Credentials.UserName.UserName = "theuser"; 
                factory.Credentials.UserName.Password = "secretpassword"; 
    
                System.ServiceModel.Description.ServiceEndpoint endpoint = new System.ServiceModel.Description.ServiceEndpoint(contract, binding, remoteAddress); 
                endpoint.Address = remoteAddress; 
    
                //var defaultCredentials = 
                //remoteAddress. 
                myWS = new MyManagerWS.MY3004527WebServiceClient(binding,remoteAddress); 
            } 
    
    . 
    . 
    .
    Not sure how to get the endpoint into the call to create the MY3004527WebServiceClient it only takes a binding and endpoint address or a configuration name and endpoint address. Not sure how to use the configuration name.

    I wonder if I can just modify my app.config to contain the user name and password?

    Here's my current app.config contents:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="MY3004527WebServiceBinding" 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://10.104.102.15:8080/MY3004527/MY3004527WebService"
                    binding="basicHttpBinding" bindingConfiguration="MY3004527WebServiceBinding"
                    contract="MyManagerWS.MY3004527WebService" name="MY3004527WebServicePort" />
            </client>
        </system.serviceModel>
    </configuration>
    Right now when I try to connect to the webservice from C# I always get the following error message...
    {"Could not connect to http://10.104.102.15:8080/MY3004527/MY3004527WebService. TCP error code 10061: No connection could be made because the target machine actively refused it 10.104.102.15:8080. "}

    I assume it is because I haven't specified the user and password info correctly.

    This seems like it shouldn't be that complicated and is driving me crazy.
    Please let me know if you know how to do this in C#.

    Thanks.

  2. #2
    Join Date
    Jan 2010
    Posts
    2

    Re: consuming a webservice that requires login credentials

    OK, I found the solution to my own problem here...

    I'll post it in case anyone is interested in what I found out.

    The first thing I did wrong was that in Visual Studio I added a Service Reference instead of a Web Service Reference. The next thing was just a simple matter of specifying credentials correctly with the NetworkCredential object after this and then setting the Url info. (If I didn't set the Url info here it would try to use a numeric IP for some reason which was failing.)

    Code:
        class Program
        {
            MyManagerWS.MY3004527WebServiceService myWS;
            
            public void initialize()
            {
                myWS = new MYManagerWS.MY3004527WebServiceService();
                myWS.Credentials = new System.Net.NetworkCredential("theuser", "secretpassword");
                myWS.Url = "https://myservice.someplace.com/MY3004527/MY3004527WebService";
                
            }
    .
    .
    .
    Thanks.

Similar Threads

  1. Replies: 4
    Last Post: 20-07-2012, 12:17 PM
  2. Replies: 7
    Last Post: 18-06-2010, 12:05 AM
  3. Replies: 2
    Last Post: 20-04-2010, 08:48 PM
  4. Windows 7 requires twice login access to desktop
    By Abhibhava in forum Operating Systems
    Replies: 5
    Last Post: 22-12-2009, 04:49 PM
  5. Replies: 3
    Last Post: 16-03-2008, 07:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,101,025.15293 seconds with 17 queries