How to add custom security binding credentials to a web service reference? - c#

I'm new to Service Reference stuff so I might need simple explanation.
I got a WSDL:
https://test.servicebench.com/servicebenchv5/services/CRMServiceOrderService?wsdl
I added the Service Reference in my project via MVS2013 without any problem.
Here's the autogenerated app.config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CRMServiceOrderBinding">
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint
address="https://test.servicebench.com/servicebenchv5/services/CRMServiceOrderService"
binding="customBinding"
bindingConfiguration="CRMServiceOrderBinding"
contract="ServiceBenchReference.CRMServiceOrderPortType"
name="CRMServiceOrderPort" />
</client>
</system.serviceModel>
</configuration>
Now, every requests I try to make to the server replies me : "Unauthorized".
I pretty sure it's because I didn't passed the required credentials in the SOAP request header.
I need to pass it 3 credentials (or info?) : ServiceBenchID, UserID and password.
I've tried using :
CRMServiceOrderPortTypeClient ServiceOrder = new CRMServiceOrderPortTypeClient();
ServiceOrder.ClientCredentials.UserName.UserName = myUserID;
ServiceOrder.ClientCredentials.UserName.password = myPassword;
but it didn't work. I checked the request via Fiddler and I didn't saw the credentials passed in the header.
So here's my question : How can I get these 3 custom credentials information to be passed in the header correctly ? (So I get a response from the server)

After searching a couple more hours, I manage to find a suitable solution !
Thanks to Thorarin for the piece of code,
How can I pass a username/password in the header to a SOAP WCF Service
It seems that having custom bindings in the app.config file is forcing you to add the security elements manually. I don't think its the best way to do it, but its working like this.

Related

Service Reference Auto Generated Duplicate Binding Configurations in App.Config in ASP.Net

I have two different third parties wsdl files. When I add service references to my project. It creates endpoints automatically in app.config. These endpoints are
<endpoint address="http://tgif1.bestovernite.com:10010/web/services/TQUOTEAPI.TQUOTEAPIHttpSoap11Endpoint/"
binding="basicHttpBinding" bindingConfiguration="TQUOTEAPISoap11Binding"
contract="OverniteLTL.TQUOTEAPIPortType" name="TQUOTEAPIHttpSoap11Endpoint" />
<endpoint address="http://208.98.171.28:10021/web/services/TQUOTEAPI.TQUOTEAPIHttpSoap11Endpoint/"
binding="basicHttpBinding" bindingConfiguration="TQUOTEAPISoap11Binding1"
contract="DiamondLineService.TQUOTEAPIPortType" name="TQUOTEAPIHttpSoap11Endpoint1" />
Please note that bindingConfiguration attributes are same just a difference is that 1 is appending in second one. just to make it unique.
When I make a client of second endpoint in the code to create request then it throws the following exception.
The binding at system.serviceModel/bindings/basicHttpBinding does not have a configured binding named 'TQUOTEAPISoap11Binding'. This is an invalid value for bindingConfiguration.
While googling I found a solution for this exception. add the binding tag in web.config in this way.
<bindings>
<basicHttpBinding>
<binding name="TQUOTEAPISoap11Binding1"/>
</basicHttpBinding>
</bindings>
It does not work for me and I am getting the same exception.
I think, Diamondline service(second endpoint) client trying to find TQUOTEAPISoap11Binding in config file, but it has been created for first one. It is just my thought, I am not confirmed about it.
I will be thankful to you, if you help me in this regard.

Trying to get ListItems from SharePoint with Windows Forms

I'm trying to create a small Windows Forms Application that uses data from a SharePoint List.
I've created a Service Reference which gave me all the SOAP-methods I need. It looks similar as to when I insert the WSDL in SoapUI, so I'm assuming the reference is correct as it is.
Afterwards I created a class in my DAL that you can view below:
public class Studies
{
//Constants
private const string _StudyListGuid = "{DA154DB9-1BE8-4C80-B94E-7EE4B8109905}";
private const string _StudyViewGuid = "{909C4BED-FB5F-49E5-ABE9-375BA15BBBFF}";
public List<Study> GetStudies()
{
var studyRequest = CreateRequest(); // <-- Fills in the constants in a GetListItemsRequest variable
var soap = new ListsSoapClient();
if (soap.ClientCredentials != null)
{
//soap.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
//soap.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
soap.ClientCredentials.Windows.ClientCredential.UserName = "username";
soap.ClientCredentials.Windows.ClientCredential.Password = "pwd";
}
//ERROR HAPPENS ON THIS LINE
var studyResponse = soap.GetListItems(studyRequest.Body.listName, studyRequest.Body.viewName, studyRequest.Body.query,studyRequest.Body.viewFields,studyRequest.Body.rowLimit, studyRequest.Body.queryOptions, studyRequest.Body.webID);
MessageBox.Show(studyResponse.ToString());
return TranslateResponse(studyResponse); //<-- Not yet implemented
}
}
And here's my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ListsSoap">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
<binding name="ListsSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://website.com/_vti_bin/Lists.asmx"
binding="basicHttpBinding" bindingConfiguration="ListsSoap"
contract="SharepointService.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>
</configuration>
Now, for the actual issue. Whenever I run my application and I call the GetStudies() method I get an error saying:
An unhandled exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll
Additional information: The HTTP request is unauthorized with client authentication scheme 'Ntlm'. The authentication header received from the server was 'NTLM'.
With an InnerException that says:
The remote server returned an error: (401) Unauthorized.
Now that all that has been said, I think the problem is that the request I'm sending out isn't authorized (duh). But I thought it would be.. (see code from Studies.cs).
So my first question is: Do I still need to authorize for the request, and if so, how do I do that?
If not, how would I be able to get the ListItems that I want?
I'm sorry for the long post but this is my first time using C# to get SharePoint data and I have no idea what data is actually important and what isn't so I just posted everything (I think). So if anyone has some feedback as to what information is obsolete and which necessary, I'm happy to edit the post.
Kind regards
Gravinco

Errors calling web service, " more than one endpoint configuration for that contract" or "no endpoint listening"

i just had a problem adding webservice that its solved in this thread
error on adding webservice
now i just have a problem by calling its method
this is my app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="sedardIPSoap" />
</basicHttpBinding>
<customBinding>
<binding name="sedardIPSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://service.proapp.ir/service/sedardIP.asmx"
binding="basicHttpBinding" bindingConfiguration="sedardIPSoap"
contract="sedardip_set.sedardIPSoap" name="sedardIPSoap" />
<endpoint address="http://service.proapp.ir/service/sedardIP.asmx"
binding="customBinding" bindingConfiguration="sedardIPSoap12"
contract="sedardip_set.sedardIPSoap" name="sedardIPSoap12" />
</client>
</system.serviceModel>
so when i try calling my function like this
sedardip_set.sedardIPSoapClient nm = new sedardip_set.sedardIPSoapClient();
nm.set_ip("x1", "x2", "x3");
it throws this exception
An endpoint configuration section for contract 'sedardip_set.sedardIPSoap'
could not be loaded because more than one endpoint configuration for that
contract was found. Please indicate the preferred endpoint configuration
section by name.
so i tried deleting one of the endpoints from app.config, after that it throws this exception
There was no endpoint listening at http://service.proapp.ir/service/sedardIP.asmx
that could accept the message. This is often caused by an incorrect address or
SOAP action. See InnerException, if present, for more details.
inner exception
{"The remote server returned an error: (404) Not Found."}
i just run service on my visual studio (locally) and by deleting second endpoint its just worked !
so can anyone plz help me to get this working on http://service.proapp.ir/service/sedardIP.asmx plz??
Is there a reason you need to use a web reference? A service reference is the updated, newer way of doing things, but if you need to use the web reference for some reason then that's fine too.
If you go the service reference route you can specifically name the endpoint you wish to use, for example:
sedardip_set.sedardIPSoapClient nm = new sedardip_set.sedardIPSoapClient("sedardIPSoap12");
nm.set_ip("x1", "x2", "x3");
It may have a new name after setting up the service reference but you would just substitute the new name in place of the sedardIPSoap12.

HTTP request is not allowed for client authentication scheme "Ntlm"

I found this question, but it seems like not my way:(
I have client service (console app), and at server side- Bitrix CMS web-service.
So, all works well , but when i update service someday on my VS 2012 and
call Send(...) method- i got error:
Received from the server authentication header "NTLM". HTTP request is not allowed for client authentication scheme "Ntlm". Received from the server authentication header "NTLM".
(it is google translate)
I try to delete and recreate service, restart VS, restart Windows- it is not working.
So, app.config :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CTfsTasksWSBinding">
<security mode ="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://server.domen.local/tfs_tasks_ws.php"
binding="basicHttpBinding" bindingConfiguration="CTfsTasksWSBinding"
contract="Bitrix.CTfsTasksWSInterface" name="CTfsTasksWSSoap" />
</client>
</system.serviceModel>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source = |SQL/CE|" />
</connectionStrings>
</configuration>
So, my code:
Bitrix.CTfsTasksWSInterfaceClient cll = new Bitrix.CTfsTasksWSInterfaceClient();
cll.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(loginn, passwd);
cll.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
I create new simple console app and paste some code from main app- i have the same error.
Please help me to fix this problem.
Thank you.
I have not valid password.
All works.

Namespace is not supported with rpc\\literal SOAP. The wrapper element has to be unqualified

I'm currently working on an integration with a leasing service provider, which runs (I assume) a Java service.
When I add the service reference in Visual Studio 2012, the reference is created correctly and I can call the methods specified in the service.
The problem arises when I get a response from the service.
Let's say I call the service with wrong parameters getCalculation and I get the JSON response JSONException. The problem is, that Visual Studio throws an exception There was an error reflecting 'JSONException'. and as InnerException: {"Namespace='http://service.ecommerce.cetelem.hu/' is not supported with rpc\\literal SOAP. The wrapper element has to be unqualified."}
This is the web.config code:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="EcommerceServiceImplPortBinding">
<security mode="Transport" />
</binding>
<binding name="EcommerceServiceImplPortBinding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://ecomdemo.cetelem.hu:443/ecommerce/EcommerceService"
binding="basicHttpBinding" bindingConfiguration="EcommerceServiceImplPortBinding"
contract="CetelemInstallmentsService.EcommerceService" name="EcommerceServiceImplPort" />
</client>
</system.serviceModel>
If this is of any help, I'm using WebAPI for the user "front-end".
Thank you for all the answers!
I figured this thing out eventually, but with the help of another post on SO: SOAP Requests in .net
All I needed to change in the service refence file was:
[System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Rpc, SupportFaults = true)]
To:
[System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Document, SupportFaults = true)]

Categories