3

My problem is in calling authenticated (has username and password) web service (asmx) in ASP.NET MVC

I did the following:

  1. I added the web service to the project by adding service reference to the solution
  2. Tried to create proxy class of the service by using wsdl.exe tool using the following command syntax

    wsdl /username:******** /password:************* /domain:********** web service url.asmx?wsdl

but it gave me the following error:

There was an error downloading web service url://???.asmx?wsdl. The request failed with HTTP status 401: Unauthorized.

I can view the service in browser by giving username and password.

When I search for using svcutil.exe to create proxy class I found that it can't be used for authenticated web service.

I want to ask if there is another way to create proxy class , or if we can call authenticated web service in asp.net mvc application without proxy class.

balexandre
  • 73,608
  • 45
  • 233
  • 342
RProgrammer
  • 47
  • 1
  • 8
  • Where did you read that svcutil can't handle authenticated services? – John Saunders Jul 15 '13 at 23:16
  • thanks very much Mr John for reply me, when I return to the following link http://msdn.microsoft.com/en-us/library/aa347733.aspx, I didn't find and thing that related to the username/pass parameter and when saw some related questions like in the following link http://stackoverflow.com/questions/2693394/passing-username-and-password-to-svcutil-exe, I found that I can't pass username/pass in svcutil command ..... thanks again – RProgrammer Jul 16 '13 at 03:30
  • Simply download the WSDL, XSD and any other related files using your browser. Then point "Add Service Reference" to the files on disk. – John Saunders Jul 16 '13 at 04:49
  • thanks again , I downloaded wsdl,xsd files from browser, and tried to run the following command svcutil service.wsdl /out:proxy.cs /config:proxy.config it gave me the following errors Could not load file or assembly 'file:///C:\temp\service.wsdl' or one of its dependencies. The module was expected to contain an assembly manifest. can you help me please – RProgrammer Jul 16 '13 at 08:15
  • I suggest using `svcutil /?`, or else "Add Service Reference", as I suggested. – John Saunders Jul 16 '13 at 09:26
  • really thank you,I know that I take from your time, but really I need your help. I tried svcutil command and tried many commands mentioned but still errors occurred , now about the second solution after adding web service to asp.net mvc how can I call methods in this service? – RProgrammer Jul 16 '13 at 10:44
  • thanks very much for supporting me to solve problem, my problem solved by deleting the first line of wsdl file... thanks again – RProgrammer Jul 16 '13 at 11:11

1 Answers1

4

I got a project with the same behavior, a Web Service (asmx) was behind a VPN and they use username/password to access it, but inside the VPN it was ok.

It will be hard to generate the proxy class directly from Visual Studio (maybe lack of support for such scenarios) but the trick is really easy as long as you can see the schema in a browser (the wsdl part).

do this:

  1. open the ?wsdl link in a browser, go to Source Code mode and copy the entire code to a file in your local computer, let'a assume c:\temp and called service.wsdl
  2. copy svcutil.exe to c:\temp (it's in .NET Framework folder, just search for it)
  3. open a terminal in c:\temp (Shift + Right Click inside the folder and choose Open command window here)
  4. run the following command: svcutil service.wsdl /out:proxy.cs /config:proxy.config

This will generate 2 files, the proxy.cs and the proxy.config

  • proxy.cs is your web service wrapper, all you need is to add to your project
  • proxy.config has the <system.serviceModel> that you need to append to your web.config file.

Now, all you need to do is follow the generated wrapper and write something like:

// Web Service initialization and authentication
client = new MyNamespace.ServiceSoapClient();
client.ClientCredentials.UserName.UserName = cUser;
client.ClientCredentials.UserName.Password = cPass;

// do something
client.InsertCompetitor(model);

// close connection
if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
    client.Close();
Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • 1
    thank you Mr Balexandre very very much for your reply, Im sure that your procedure will work for me, but when I run svcutil command as you mentioned it gave me the following errors Cannot load file C:\temp\service.wsdl as an Assembly. Check the FusionLogs for more Information. Could not load file or assembly 'file:///C:\temp\service.wsdl' or one of its dependencies. The module was expected to contain an assembly manifest. .. I don't know why this error occurred , I searched for this error and tried for all possibilities that may gave the error , but it was failed ... thanks again – RProgrammer Jul 16 '13 at 03:38
  • 1
    you need to copy the source code as is, you can't copy the code you see from the browser window, you need to open the "View source code" and you need to check if there's any space chars in the beginning of the file and remove them, as it needs to be a valid wdsl file. – balexandre Jul 16 '13 at 10:17
  • it worked it worked , thanks really really thanks , only I delete the first line which is , and svcutil created the two files, I will continue your process now – RProgrammer Jul 16 '13 at 10:53
  • There was an empty line and after deletion i could see two proxies getting generated. – Sandeep Aug 11 '15 at 20:40