Stacked up with web service configuration - c#

I'm currently stacked with the web service that im creating right now. when Testing it in local it all works fine but when I try to deploy it to the web server it throws me the following error
An error occurred while trying to make a request to URI '...my web service URI here....'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.
here is my web config.
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
<validation validateIntegratedModeConfiguration="false" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2000000000" />
</requestFiltering>
</security>
</system.webServer>
<connectionStrings>
<add name="........" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<!-- Testing -->
<add key="DataConnectionString" value="..........." />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</buildProviders>
</compilation>
<httpRuntime executionTimeout="1200" maxRequestLength="2000000" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="Service1">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2000000000" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="nextSPOTServiceBehavior">
<serviceMetadata httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2000000000" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="SecureBasic" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="Transport" />
<readerQuotas maxArrayLength="2000000" maxStringContentLength="2000000"/>
</binding>
<binding name="BasicHttpBinding_IDownloadManagerService" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="nextSPOTServiceBehavior" name="NextSPOTDownloadManagerWebServiceTester.Web.WebServices.DownloadManagerService">
<endpoint binding="basicHttpBinding" bindingConfiguration="SecureBasic" name="basicHttpSecure" contract="NextSPOTDownloadManagerWebServiceTester.Web.WebServices.IDownloadManagerService" />
<!--<endpoint binding="basicHttpBinding" bindingConfiguration="" name="basicHttp" contract="NextSPOTDownloadManagerWebServiceTester.Web.WebServices.IDownloadManagerService" />-->
<!--<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDownloadManagerService" contract="NextSPOTDownloadManagerWebServiceTester.Web.WebServices.IDownloadManagerService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />-->
</service>
</services >
</system.serviceModel>
</configuration>
Client access Policy
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*"/>
<domain uri="https://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
CROSS Domain policy
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain- policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

This sounds like the error you get when you are trying to use silverlight to access a webservice from where it is not hosted. If this is the case you need to add a clientaccesspolicy.xml to your web application folder.
MSDN article on allowing crossdomain access for a silverlight application.

Related

WCF - HTTP request was aborted

I am new to WCF and trying to troubleshoot an error. I have a WCF service running on a remote server and am receiving an error when another WCF service tries to call it running in my localhost.
The HTTP request to [url to the service] was aborted. This may be due to the local channel being closed while the request was still in progress. If this behavior is not desired, then update your code so that it does not close the channel while request operations are still in progress.
I tried looking at the activity in the Microsoft Service Trace Viewer and the first error received is:
Failed to lookup a channel to receive an incoming message. Either the endpoint or the SOAP action was not found.
I can see the service is running, I can access it in the browser.
Can anyone help point me in the right direction of anything I can try? The service that I am trying to connect to is called LinkTable and I have added the relevant config files.
Client web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6"/>
<httpRuntime targetFramework="4.6"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyService" />
<binding name="BasicHttpBinding_IKeyLinkage" closeTimeout="00:25:00"
openTimeout="00:02:00" receiveTimeout="00:02:00" sendTimeout="00:02:00"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
<binding name="BasicHttpBinding_IMaintenance" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_L2" closeTimeout="00:25:00" openTimeout="00:02:00"
receiveTimeout="00:02:00" sendTimeout="00:02:00" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
<binding name="wsHttpBinding_IProcessXML" closeTimeout="00:25:00"
openTimeout="00:02:00" receiveTimeout="00:02:00" sendTimeout="00:02:00"
maxReceivedMessageSize="2147483647" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http:/path/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="NotificationService.IMyService" name="BasicHttpBinding_IMyService" />
<endpoint address="http://path/Maintenance.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMaintenance"
contract="MaintenanceService.IMaintenance" name="BasicHttpBinding_IMaintenance" />
<endpoint address="http://path/Services/KeyLinkage.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IKeyLinkage"
contract="KeyLinkageService.IKeyLinkage" name="BasicHttpBinding_IKeyLinkage" />
<endpoint address="https://path/Services/LinkTableService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_L2"
contract="LinkTable_L2.ILinkTableService" name="WSHttpBinding_ILinkTableService" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="wsHttpBinding" scheme="http" bindingConfiguration="wsHttpBinding_IProcessXML"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
<source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp" />
</sharedListeners>
</system.diagnostics>
</configuration>
Server web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<system.serviceModel>
<client>
<endpoint address="http://path/Services/LinkTable.svc" binding="basicHttpBinding" bindingConfiguration="AppName_WCF_BasicHttpBinding" contract="LinkTableWCF.ILinkTable" name="BasicHttpBinding_ILinkTable" />
</client>
<services>
</service>
<service behaviorConfiguration="AppName_L2_WCF_Behavior" name="AppName_L2_WCF.Services.LinkTableService">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="AppName_L2_WCF_wsHttpBinding" contract="AppName_L2_WCF.ServiceContracts.ILinkTableService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="AppName_WCF_BasicHttpBinding" maxBufferPoolSize="819200" maxReceivedMessageSize="2147483647" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="AppName_L2_WCF_wsHttpBinding" maxBufferPoolSize="819200" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="AppName_L2_WCF_Behavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="AppName_L2_WCF.Utils.ServiceAuthenticator, AppName_L2_WCF" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="wsHttpBinding" scheme="http" bindingConfiguration="AppName_L2_WCF_wsHttpBinding" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>

WCF used as service reference: how do it?

I've my RESTful service hosted here .
I can use it perfectly with get&post http request.
How can I use as "service reference" in a Visual Studio project? I have added it in my test project and I see the methods correctly, but, when I try to call on of them, I get the "could not find default endpoint element that references contract in the servicemodel client" error, with http code 404.
So, I'm trying to modify my web.config file, but I get only different errors. For example, now I have try to add a SOAP endpoint,but I get a "bad request" error.
This is one of my methods (they are defined in the same way)
[OperationContract]
[WebGet(UriTemplate = "impianto.json?retista={codret}&storeID={storeID}&H={hashedString}", ResponseFormat = WebMessageFormat.Json)]
List<WrapImpianti> GetImpiantoJSON(string codret, string storeID, string hashedString);
and this is the web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="ServerAddress" value="37.59.146.241:8099/isOverviewExport.svc" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="isOverviewExport.Service1" behaviorConfiguration="MyBehavior">
<endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" name="s" contract="isOverviewExport.isOverviewExport"/>
<endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" name="base" contract="isOverviewExport.isOverviewExport">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="SecurityByTransport" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<!--<enableWebScript/>-->
<webHttp helpEnabled="true" />
<CorsSupport />
</behavior>
<behavior name="soapBehavior">
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyBehavior">>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/AccessData.Model.csdl|res://*/AccessData.Model.ssdl|res://*/AccessData.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=myds;initial catalog=mydb;persist security info=True;user id=myuser;password=mypwd;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
You can also try yourself adding the service reference (the link is the same as above, http://37.59.146.241:8097/isOverviewExport.svc ). Why the second line of this snippet gives me the error I wrote above?
var svc = new ServiceReference5.isOverviewExportClient();
var ret = svc.GetImpiantoXML("123456", "AAA111", "test");
So, how can I use it as service reference in VS? I was thinking I have to set the soap endpoint, but maybe it isn't correct. Can you help me? :)

IIS 7 ERR_CONNECTION_TIMED_OUT only with public IP

I'm working with WCF on IIS 7.
Previously, I have done a WCF library and set it to work on IIS 7. It works well, also if I use it with our public IP.
Then, I have done a WCF application and set it to work on the same IIS 7. The address is the same, the difference is in the port. This WCF works when I call it with the internal IP, but is I use the external IP, I get a timed out error.
You can try yourself, this is the pubblic address:
http://77.108.40.77:8096/
(if you put 8095 as port, it works).
Why am I getting this? (ERR_CONNECTION_TIMED_OUT)
This is my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.``0, Culture=neutral, ``PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="MultipayOnline.Service1" >
<!--bindingConfiguration="SecurityByTransport"-->
<!--<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecurityByTransport" name="base" contract="CardServiceLib.ICardService">-->
<endpoint address="" name="base"
behaviorConfiguration="restBehavior"
binding="webHttpBinding"
contract="MultipayOnline.IMultiPOService" >
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="SecurityByTransport" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" >
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<!--<enableWebScript/>-->
<webHttp helpEnabled="true"/>
<CorsSupport />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" />
</behavior>
<!--<behavior name="customBehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MultipayOnline.CustomPass, MultipayOnline"/>
</serviceCredentials>
</behavior>-->
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
Per sfogliare la directory radice dell'applicazione Web durante il debug, impostare il valore riportato sotto su true.
Impostarlo su false prima della distribuzione per evitare di diffondere informazioni sulla cartella dell'applicazione Web.
-->
<directoryBrowse enabled="true" />
<!--<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>-->
</system.webServer>
<connectionStrings><add name="isMultiPayOnLineEntities" connectionString="..." providerName="System.Data.EntityClient" /> </connectionStrings>
Can you help me? Can I post something to you for make clearer the trouble?
I think your port is closed .Check your IP and Port in http://ping.eu/port-chk/ if its status is closed then this msg will be shown . Plz open this Port to solve this problem..

Basic Authentication for WCF

I am trying to do a very basic but secure username/password authentication with wcf.
However when I look at the value of the ServiceSecurityContext.Current.PrimaryIdentity; it contains the credentials of my windows machine and claims it is authorised (even though I have not yet done any authorisation) instead of the username and password I provided to the service.
My web.config of the service is as follows
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WsHttpBindingConfig">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<protocolMapping>
<add binding="wsHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
and the app.config of the client app is as follows
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WcfSecuredService/Service1.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
I call the service with the following code
ServiceReference1.Service1Client clnt = new ServiceReference1.Service1Client();
clnt.ClientCredentials.UserName.UserName = "peter";
clnt.ClientCredentials.UserName.Password = "grr";
string result=clnt.GetSecuredData();
What am I doing wrong?
Please note that both the client app and the service are both running on the same machine. I do not know if the identity is that of the machine running the service or the one passed to it from the client as they are both the same credentials.....
I suppose the other question is possibly "How do I get the username and password that were passed to the service?"
I have worked this out now
I needed to create a custom validation class which I found here How to: Use a Custom User Name and Password Validator
I also needed to make a few changes to the web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1Secure.Auth,WcfService1Secure" />
</serviceCredentials>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WsHttpBindingConfig">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<protocolMapping>
<add binding="wsHttpBinding" scheme="https" bindingConfiguration="WsHttpBindingConfig" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
and the 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="BasicHttpBinding_IService1" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost/WcfService1Secure/Service1.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
contract="ServiceReference1.IService1" name="WSHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
Now the user is validated on the request and the username is available using
ServiceSecurityContext.Current.PrimaryIdentity;
hi you should add a method that can accept login and password on your server side and store them in a variables or db
your method should look like this
ServiceReference1.Service1Client clnt = new ServiceReference1.Service1Client();
clnt.ClientCredentials.UserName.UserName = "peter";
clnt.ClientCredentials.UserName.Password = "grr";
clnt.SignUp(clnt);
or
clnt.SignUp(login,password);
what you are doing can work on a standalone application but not in a webcall
your call to method service will have something like
http://MyService/IMyContract/MyAction1
Hope this help
If you are using PerSession instance then you can call first Authenticate on server and on successful maintain a flag on server, subsequent call will check for the flag before executing. that means client need to make first call to Authenticate method and then only call other methods.
If not using PerSession instance you can create own proxy class for the server and while creating proxy instance accept LoginName/Password which it can pass in header on each call and on server side implement custom ServiceAuthorizationManager, it can retrieve credential from OperationContext.Current.IncomingMessageHeaders and validates it.

Ajax Enabled WCF service with Windows Authentication works on Local But not when Hosted

I have a WCF ajax enabled web service that uses Windows Authentication, when I run the application on my local PC it works perfectly, but when I host it does not want to pass through the client credentials to the service.
I keep getting this error:
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
I believe I have included all the necessary configuration but I am not sure.
My Web Config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<system.web>
<authentication mode="Windows"/>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<connectionStrings>
<add name="reportEntities" connectionString="metadata=res://*/Entities.TNTReport.csdl|res://*/Entities.TNTReport.ssdl|res://*/Entities.TNTReport.msl;provider=System.Data.SqlClient;provider connection string="data source=UECZATNT02;initial catalog=report;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<!--<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PassThrough">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="TNTQueryEngine.Services.InformationServicesAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="TNTQueryEngine.Services.InformationServices">
<endpoint address="" behaviorConfiguration="TNTQueryEngine.Services.InformationServicesAspNetAjaxBehavior"
binding="webHttpBinding" contract="TNTQueryEngine.Services.InformationServices" />
</service>
</services>
</system.serviceModel>-->
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="MyBindingForWindowsAuth">
<security mode="TransportCredentialOnly">
<!-- For Ntlm to work do => IIS right click Win Auth => Providers => Move NTLM up -->
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="UseAspNetRoles" impersonateCallerForAllOperations="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="XYZ">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="TNTQueryEngine.Services.InformationServices" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyBindingForWindowsAuth"
contract="TNTQueryEngine.Services.InformationServices" behaviorConfiguration="XYZ" />
<endpoint address="mex" binding="webHttpBinding" bindingConfiguration="MyBindingForWindowsAuth" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
My WCF Service with 1 Method:
namespace TNTQueryEngine.Services
{
[ServiceContract(Namespace = "InfoServices")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class InformationServices
{
reportEntities RP = new reportEntities();
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public List<Receiving> GETReceiving(DateTime DATE)
{
return (from SS in RP.spQueryEngineGETReceivingSummary(DATE)
where SS.QTY > 0
select new Receiving
{
ReceivedDate = DATE,
Model = SS.Model,
Qty = (int)SS.QTY
}
).ToList();
}
}
}
Really not sure what could be going wrong. Thanks in advance for the assistance

Categories