WCF Windows Authentication, local debug ok but not work on server - c#

On my Server, running a Windows Authentication WCF application hosted in IIS 7:
IIS Config: Windows Auth : disabled; anonymous: enabled
<system.serviceModel>
<services>
<service name="Services.AccountService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="ws2007HttpBinding" bindingConfiguration="AccountServiceBinding"
contract="Contracts.IAccountService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!--<serviceDebug includeExceptionDetailInFaults="true"/>-->
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<ws2007HttpBinding>
<binding name="AccountServiceBinding" >
<security mode="Message">
<message clientCredentialType="Windows" />
</security>
</binding>
</ws2007HttpBinding>
</bindings>
</system.serviceModel>
and then I create a MVC4 Application with following code to call WCF service:
WS2007HttpBinding myBinding = new WS2007HttpBinding();
myBinding.Security.Mode = SecurityMode.Message;
EndpointAddress endpoint = new EndpointAddress("http://server/accountservice.svc");
AccountServiceClient _client = new AccountServicesObjects.AccountServiceClient(myBinding, endpoint);
_client.ClientCredentials.Windows.ClientCredential.UserName = "username";
_client.ClientCredentials.Windows.ClientCredential.Password = "password";
var user = _client.GetUserInformation(); // works fine
After I finished this mvc4 application, I deploy this website to the Same server which is running WCF one, when I login, occurs:
System.ServiceModel.Security.SecurityNegotiationException: The caller was not authenticated by the service.
System.ServiceModel.FaultException:The request for security token could not be satisfied because authentication failed.
on System.ServiceModel.Security.SecurityUtils.ThrowIfNegotiationFault(Message message, EndpointAddress target)
on System.ServiceModel.Security.SspiNegotiationTokenProvider.GetNextOutgoingMessageBody(Message incomingMessage, SspiNegotiationTokenProviderState sspiState)
how this happend?

It seems your service hasn't been started at all. If you enable tracing you can find an exception. There is an error in your configuration - you have no base address, but you have set HttpGetUrl to true (for that option you have to set base address)
This should work for you:
<service name="Services.AccountService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="accountservice.svc" binding="ws2007HttpBinding"
bindingConfiguration="AccountServiceBinding"
contract="Services.IAccountService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
</service>

Related

WCF Serice - Getting 400 Bad Request but can use IE to browse to it

I have written a WCF Service which is being called by a Winforms application.
I have tested calling the WCF Service from the WinForms application locally and it all works fine
I have moved both the WinForms application and the WCF Service to a Remote Server, on the Remote Server I can use IE to browse to the Service but when I try to use the Winforms application I get a 400 Bad Response error.
My local machine and the Remote Server has been configured exactly the same, Windows Firewall, User accounts etc and the codebase/config files of both the Service and Winforms app are the same.
The config file for the WCF Service is as follows (i've had to remove the base address)
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="CodeLocksAPIServiceBehavior" name="CodeLocksAPI.WCF.CodeLocksAPIService">
<host>
<baseAddresses>
</baseAddresses>
</host>
<endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="CodeLocksAPI.WCF.ICodeLocksAPIService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CodeLocksAPIServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
In the Winforms app config I have tried adding the DefaultProxy tabs which do not help
WCF Service is called from Code Using the following
string serviceUserName = ConfigurationManager.AppSettings["ServiceUserName"];
string servicePassword = ConfigurationManager.AppSettings["ServicePassword"];
HttpClientHandler handler = new HttpClientHandler
{
Credentials = new
System.Net.NetworkCredential(serviceUserName, servicePassword)
};
this.Client = new HttpClient(handler);
this.Client.BaseAddress = new Uri(this.GetBaseAddressFromConfig());
processInitializeLockRequestArgs.AssetRef = assetRef;
string contentMessage = Newtonsoft.Json.JsonConvert.SerializeObject(processInitializeLockRequestArgs);
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(contentMessage);
ByteArrayContent content = new ByteArrayContent(contentBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
httpResponse = this.Client.PostAsync(methodUri, content).Result;
As already stated - this code is working locally but not on the remote server - the WCF Service can be browsed to locally and on the remote service
I've been informed that the issue is most likely down to Proxy settings on the WinForms application and that the issue is not down to a Firewall or anything like that
You could try adding
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
....
to the beginning of your WinForms config file.

EndpointNotFoundException with WCF client in same Assembly

I'm hosting a WCF Service in my WPF app and want to consume it within the same assembly (in another instance, though). For the sake of dependency injection I'm calling ServiceHost.Open with an already created instance of my service implementation.
Contract and Service implementation
[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IService
{
[OperationContract]
PartnerInfo GetPartnerInfo();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
private IpService _ipService;
public Service(IpService ipService)
{
_ipService = ipService;
}
public PartnerInfo GetPartnerInfo()
{
var info = new PartnerInfo();
info.Address = _ipService.GetLocalIpAddress();
return info;
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Project.WebService.Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="endpoint0"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService"
contract="Project.WebService.IService">
<identity>
<servicePrincipalName value="Project.WebService.Service"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:61000/Project/Service.svc" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService"
transferMode="Buffered"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
sendTimeout="00:30:00"
receiveTimeout="infinite">
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
</security>
<reliableSession inactivityTimeout="infinite" enabled="true" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService"
contract="Project.WebService.IService"
name="NetTcpBinding_IService">
</endpoint>
</client>
</system.serviceModel>
</configuration>
ServiceHost creation
_serviceHost = new ServiceHost(_Service);
_serviceHost.Open();
After opening the ServiceHost, it listens on Port 61000 (checked via netstat). Altough I don't need it (because I created a client implementing IService myself), I tested creating a service reference and it worked.
However, my service implementation looks like this:
public class ServiceClient : ClientBase<IService>, IService
{
public ServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public PartnerInfo GetPartnerInfo()
{
return Channel.GetPartnerInfo();
}
}
And when I'm calling it this way, with another service hosting instance on the same machine, an EndpointNotFoundException is thrown:
Uri baseUri = new Uri("net.tcp://<IPAdress>:61000");
Uri uri = new Uri(baseUri, "Project/Service.svc");
// Spn identity is only here for testing purposes, doesn't work either way
return new ServiceClient("NetTcpBinding_IService", new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity("NetTcpBinding_IService")));
Am I missing something on my implementation of the ServiceClient as I have both service and client in the same assembly and therefore don't want to create a service reference in Visual Studio?
Update
I tested the whole process creating a Service Reference. This doesn't work either. I also double-checked my firewall rules. Here is the exception message:
There was no endpoint listening at net.tcp://192.168.200.29:61000/Project/Service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Update 2
For testing purposes, I created a second wsHttpBinding Endpoint. This one is reachable in my browser, so the service is up and running.
After hours of debugging, I solved the (quite simple) issue myself:
As it turned out, the service URL wasn't net.tcp://localhost:61000/Project/Service.svc but net.tcp://localhost:61000/Project/Service.svc/endpoint0.
After removing the "endpoint0" from my endpoint configuration, the connection went well:
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService"
contract="Project.WebService.IService">
<!-- -->
</endpoint>

Https wcf webservice Error

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
<behavior name="webHttpBehavior">
<webHttp />
</behavior></endpointBehaviors>
</behaviors>
<services>
<service name="Implementation.Service" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="Contract.IService" behaviorConfiguration="web" bindingConfiguration="basicHttpBinding"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="CodeItSoap" closeTimeout="00:01:00">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType ="Basic" realm =" "/>
<message clientCredentialType= "username" algorithm ="default">
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://******.****-***/*****_*****?SOAP"
binding="basicHttpBinding" bindingConfiguration="CodeItSoap"
contract="Service.CodeItSoap" name="CodeItSoap" />
</client>
</system.serviceModel>
</configuration>
This is just a sample of my web.config file. When I run the service in the local host it runs fine and with the help of wcf test client I am getting the required output.
But when I put the dlls & web.config in the server where I have to host the service it's throwing an error
"Could not find a base address that matches scheme HTTP for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https]"
Could any one tell me what the reason for the above error is?
General Flow of my web service
Application -> Server hosting(calc.svc) -> https://******.****-*/*****_*****?SOAP(authenticated)
when i add the service reference app.config got generated and by default basichttpbinding got added to app.config file.
As per my understanding web.config file is used to host the service in iis & i think my web.config is wrong.
In the client end point what should be the end point to calc.svc or https://*?soap?
Is the basichttpbinding ok for the https://prd36/calc.svc url?
do i need to specify one more binding for the application too?
Please help me understand i am heavily confused as the web.config which i have edited is a existing one which is still running the old service reference.
<serviceMetadata httpsGetEnabled="true"/>
<endpoint address="json" binding="webHttpBinding" contract="Contract.IService" behaviorConfiguration="web"></endpoint>
<endpoint address="mex" binding="mexHttpsinding" contract="IMetadataExchange"/>
above changes i did and the service url is running in web browser.
Use http:// instead of https:// in your endpoint address.
OR
User BasicHttpsBinding instead of BasicHttpBinding in your endpoint.
follow this link if the problem still exist.

create net.tcp service reference in client app using VS2015

I am using VS2015 and creating a WPF client app for a web service. The service is running on IIS on my network on a dedicated web server. When I try to add a service reference I get an error message that says it cannot recognize the uri prefix.
The web service web.config file is as follows:
<system.serviceModel>
<diagnostics wmiProviderEnabled="true">
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding-Streamed" transferMode="Streamed" maxReceivedMessageSize="1073741824">
<readerQuotas maxStringContentLength="8192000" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceAuthorization principalPermissionMode="UseWindowsGroups" />
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="[Namespace].Services.IIS">
<endpoint address="net.tcp://[server:port]/[website on iis]/Shared/Shared.svc" bindingConfiguration="netTcpBinding-Streamed" binding="netTcpBinding" contract="[Namespace].Services.Interfaces.Shared.IShared" />
<endpoint address="net.tcp://[server:port]/[website on iis]/UITax/Reports.svc" bindingConfiguration="netTcpBinding-Streamed" binding="netTcpBinding" contract="[Namespace].Services.Interfaces.UITax.Reports.IReport" />
<endpoint address="net.tcp://[server:port]/[website on iis]/UITax/Assignments.svc" bindingConfiguration="netTcpBinding-Streamed" binding="netTcpBinding" contract="[Namespace].Services.Interfaces.UITax.Assignments.IAssignments" />
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="errorHandler" type="[Namespace].Services.Framework.WCF.WcfErrorHandlerExtension, [Namespace].Services.WCF.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
I enter "net.tcp://[server:port]/[website on iis]/Shared/Shared.svc" into the Address box and click GO and get the error.
What am I doing wrong?
VS2015 error message:
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://[server:port]/[website on iis]/shared/shared.svc'.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:04:59.9980001'.
An existing connection was forcibly closed by the remote host
If the service is defined in the current solution, try building the solution and adding the service reference again.
as CodeCaster said in comment to the question, you should enable metadata publishing (<serviceMetadata httpGetEnabled="true" />) and add mex endpoint of type mexHttpBinding (so use http://[server:port]/[website on iis]/UITax/Reports.svc) to add service reference. OR add mex endpoint of type mexTcpBinding and use your net.tcp addresses to add service reference
Endpoints for metadata exchange:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
Or
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>

Impersonation in self-hosted WCF?

I'm currently developing a WCF service self hosted in a Windows service with the Self-Hosted SL Svc template.
The template works as it should be and I'm able to make calls from my Silverlight application, but when I tried to modify the project to use Impersonation:
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
It throws me an exception during host.Open();:
System.InvalidOperationException was
unhandled Message=The contract
operation 'GetData' requires Windows
identity for automatic impersonation.
A Windows identity that represents the
caller is not provided by binding
('CustomBinding','http://tempuri.org/')
for contract
('IService1','http://tempuri.org/'.
This is my configuration:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="binaryHttpBinding">
<binaryMessageEncoding/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<services>
<service name="SLServiceLibrary.Service1" behaviorConfiguration="SLServiceLibrary.ServiceBehavior">
<endpoint address="Service1" binding="customBinding" contract="SLServiceLibrary.IService1" bindingConfiguration="binaryHttpBinding"/>
<endpoint address="" binding="webHttpBinding" contract="SLServiceLibrary.IClientAccessPolicy" behaviorConfiguration="webHttpEnablingBehavior"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SLServiceLibrary.ServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpEnablingBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
What do I have to change to make this work? do I need to add some configuration to my Silverlight client too?
Thanks in advance :)
I haven't used custom bindings (yet), but this blog entry may give you some help: http://geekswithblogs.net/robz/archive/2007/10/03/wcf-impersonation---specifying-windows-authentication-credentials-on-the-service.aspx
Well after a long search, I've come up with two solutions, one I found at MSDN:
<bindings>
<customBinding>
<binding name="binaryHttpBinding">
<binaryMessageEncoding/>
<httpTransport authenticationScheme="Ntlm"/>
</binding>
</customBinding>
</bindings>
The other one was at the Silverlight forums:
<bindings>
<customBinding>
<binding name="binaryHttpBinding">
<binaryMessageEncoding/>
<httpTransport authenticationScheme="Negotiate"/>
</binding>
</customBinding>
</bindings>
Don't know what is the major difference between these two, I've managed to open the service and call it from Silverlight successfully. If someone call elaborate on the difference, I would appreciate it.

Categories