Retrieve domain name from WCF request - c#

I use following code to retrieve domain name from WCF operation context.
WindowsIdentity identity = OperationContext.Current.IncomingMessageProperties.Security.ServiceSecurityContext.WindowsIdentity;
domain = identity.Name.Split('\\')[0];
While its working as expected in my machine when I deploy the resulting domain name always comes as NT AUTHORITY.
Deployment server runs Windows server 2008 R2 and IIS 7. I am using basicHttpBinding and following is the binding configuration. Can any one point out anything that I am doing incorrectly?
<basicHttpBinding>
<binding name="BasicHttpBinding_IIdentityService" 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="Transport">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>

Related

Could not find endpoint element with name '' and contract '' in the ServiceModel client configuration section

I have tried days to solve this damn error. I am calling the same service in asp.net and it works fine, but when I call it in a sharepoint solution it does not work. This is the error that I receive.
Could not find endpoint element with name 'WeatherSoap' and contract 'WeatherService.WeatherSoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.
this is my app.config in the library:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WeatherSoap" 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>
<customBinding>
<binding name="WeatherSoap12">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
binding="basicHttpBinding" bindingConfiguration="WeatherSoap"
contract="WeatherService.WeatherSoap" name="WeatherSoap" />
<endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
binding="customBinding" bindingConfiguration="WeatherSoap12"
contract="WeatherService.WeatherSoap" name="WeatherSoap12" />
</client>
</system.serviceModel>
This is my C# code
WeatherService.WeatherSoapClient client = new WeatherService.WeatherSoapClient("WeatherSoap");
WeatherService.WeatherReturn result = client.GetCityWeatherByZIP(txtZip.Text);
this is the code that I use in code behind in both sharepoint visual web part and in asp.net web page. The strange thing is that it works fine in asp.net but not in sharepoint.
I'm calling the service in a same project.
Look at this link: http://msdn.microsoft.com/en-us/library/ff647110.aspx
You may need to add configure the wcf endpoint programmatically for the sharepoint web part.

How do i call webservice from windows service

I have a windows service from which I am trying to call a method under remote webservice but I am getting error that "Could not find default endpoint element that references contract 'MyWebService.BookingCitySoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
Following is the code for that
.cs File
MyWebService.BookingCitySoapClient ws = new MyWebService.BookingCitySoapClient();
ws.CallBookStatus();
ws.CallCanStatus();
App.config
<bindings>
<basicHttpBinding>
<binding name="BookingCitySoap" 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://localhost:51317/Web/BookingCity.asmx"
binding="basicHttpBinding" bindingConfiguration="BookingCitySoap"
contract="MyWebService.BookingCitySoap" name="BookingCitySoap" />
</client>

Invalid operation Exception while instantiating object of asmx web service

I am trying to incorporate .asmx web service in my application.
For this I
1. created a Class library project.
2. Added service reference with the given wsdl
3. An app.Config was automatically created. The app.config that is
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="UploadWebServiceSoap" 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>
<customBinding>
<binding name="UploadWebServiceSoap12">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:xxxx/xxx/UploadWebService.asmx"
binding="basicHttpBinding" bindingConfiguration="UploadWebServiceSoap"
contract="ServiceReference1.UploadWebServiceSoap" name="UploadWebServiceSoap" />
<endpoint address="http://localhost:xxxx/xxx/UploadWebService.asmx"
binding="customBinding" bindingConfiguration="UploadWebServiceSoap12"
contract="ServiceReference1.UploadWebServiceSoap" name="UploadWebServiceSoap12" />
</client>
</system.serviceModel>
After all the set up, I tried calling the methods in web-service in my function.
UploadWebServiceSoapClient dpcClient = new UploadWebServiceSoapClient();
This is where is get an error
Could not find default endpoint element that references contract 'ServiceReference1.UploadWebServiceSoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Can anyone please tell me why I am getting this error. Also, I understand the binding created basicHttpBinding but what is and an endpoint for the same was created
It looks as if you've created service reference multiple times. First you created ServiceReference1 and then DPCServiceReference. You should change your endpoint configuration:
contract="ServiceReference1.UploadWebServiceSoap"
to
contract="DPCServiceReference.UploadWebServiceSoap"
You have 2 lines with this error.
If this won't help try deleting customBinding and endpoint with Soap12.

Why do I get a WindowsIdentity when using message security?

When switching from no security to Certificate I get a WindowsIdentity in the IAuthorizationPolicy.Evaluate (evaluationContext.Properties.TryGetValue("Identities", out obj)? Is this by design :
From this :
<binding name="NetTcpBinding_IMyAppClientServiceRegular" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="infinite" sendTimeout="01:00:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession ordered="true" inactivityTimeout="infinite" enabled="false"/>
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
To this :
<binding name="netTcpCertificate" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="infinite" sendTimeout="01:00:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="1000" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="200" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession ordered="true" inactivityTimeout="infinite" enabled="false"/>
<security>
<message clientCredentialType="Certificate"/>
</security>
</binding>
Is there any way to avoid getting the WindowsIdentity in IAuthorizationPolicy.Evaluate? I do only want the WindowsIdentity to be set when using this binding :
<binding name="NetTcpBinding_IMyAppClientServiceWindows" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="infinite" sendTimeout="01:00:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession ordered="true" inactivityTimeout="infinite" enabled="false"/>
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
I noticed you have not set the mode in <security> in your netTcpCertificate config instead of what you probably want: <security mode="Message">. By not setting the mode attribute, WCF will use the default value of Transport instead of Message which is what you likely want for certificate credentials. It's possible that WCF is passing the Windows identity when the mode is set to transport but I haven't tried to verify it.
UPDATE:
Based on the comments below, you should make sure that the client config file endpoint>indentity>dns>value matches the name of server certificate CN= value. This value needs to be MyAppServer based on what you entered.

Web Service Creating Instance Problem -> Could not find default endpoint element that references contract 'MyWebService.ClassName'

i want to use the below Web-Service :
https://acquirer.sb24.com/ref-payment/ws/ReferencePayment?WSDL
and builded config file after adding ->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PaymentIFBinding" 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="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="PaymentIFBinding1" 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="https://acquirer.sb24.com:443/ref-payment/ws/ReferencePayment"
binding="basicHttpBinding" bindingConfiguration="PaymentIFBinding"
contract="SB24Service.PaymentIF" name="PaymentIFPort" />
</client>
</system.serviceModel>
</configuration>
i want to call verifyTransaction method of this WebService :
PaymentIFClient pic = new PaymentIFClient();
double pic_result = pic.verifyTransaction(str1, str2);
but the first line throws the below error :
Could not find default endpoint element that references contract
'SB24Service.PaymentIF' in the ServiceModel client configuration
section. This might be because no configuration file was found for
your application, or because no endpoint element matching this
contract could be found in the client element.
EDIT :
"This error can arise if you are calling the service in a class
library and calling the class library from another project." -> My
Situation is like this ...
what should i change i configuration file ?
thanks in advance
Editing wcf configuration by hand is tricky, as there are many sections that need to be correct and correctly reference each other. Try opening your app.config file in SvcConfigEditor (also available from the Tools menu in Viusal Studio). It is way easier to get things correct when having GUI help on what to do.

Categories