Hello everybody,
I have developp a Wcf sample project.
This sample works fine alone but when I want to add authentication to it, it fails.
I have set the diagnostic trace for this Wcf service but the messages I get are only :"Fail to open System.ServiceModel.ServiceHost", "System.ServiceModel.ServiceHost has generated errors" and "An error System.ServiceModel.ServiceHost has appened.
I have been based on a simple example I found on the net and I think I have reproduced it correctly.
Here is the web.config file for my Wcf service :
xml version="1.0" encoding="utf-8"?>
debug="true" targetFramework="4.0" />
>
name="basicHttpServiceBinding">
mode="TransportCredentialOnly">
clientCredentialType="Basic"/>
name="customBehavior">
httpGetEnabled="true" httpsGetEnabled="true" />
includeExceptionDetailInFaults="true" />
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfService1.UserAuthentication,WcfService1"/>
name="WcfService1.Service1" behaviorConfiguration="customBehavior">
address="http://localhost/TestServiceWCF/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="basicHttpServiceBinding"
contract="WcfService1.IService1">
value="localhost" />
address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
baseAddress="http://localhost/TestServiceWCF/" />
wmiProviderEnabled="true">
logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog="3000"
/>
multipleSiteBindingsEnabled="true" />
autoflush="true" indentsize="4" />
name="System.ServiceModel"
switchValue="All"
propagateActivity="true" >
name="xml"/>
name="System.ServiceModel.MessageLogging">
name="xml"/>
name="myUserTraceSource"
switchValue="Information, ActivityTracing, Warning, Error">
name="xml"/>
name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="E:\ldiep\Projets\Wcf\IIS_Logs\messages.svclog" />
runAllManagedModulesForAllRequests="true"/>Here is the App.config for my client application :
xml version="1.0" encoding="utf-8" ?>
name="DefaultBinding_IService1">
mode="TransportCredentialOnly">
clientCredentialType="Basic"/>
address="http://localhost/TestServiceWCF/Service1.svc"
binding="basicHttpBinding" bindingConfiguration="DefaultBinding_IService1"
contract="ServiceReference1.IService1"
name="ServiceReference1" />Here is the UserAuthentication.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.IdentityModel.Selectors;
namespace WcfService1
//namespace Service
{
public class UserAuthentication : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
try
{
if (userName == "test" && password == "test123")
{
Console.WriteLine("Authentic User");
}
}
catch (Exception ex)
{
throw new FaultException("Unknown Username or Incorrect Password");
}
}
}
}And here is my call to the Wcf service :
ServiceReference1.Service1Client serviceProxy = new ServiceReference1.Service1Client();
serviceProxy.ClientCredentials.UserName.UserName = "test";
serviceProxy.ClientCredentials.UserName.Password = "test123";
string sReturned = serviceProxy.GetData(14);Thank you very much in advance.
Eric.

Khan Abrar AhmedRank #778 · 3K reputationPosted Jun 14, 2015, 2:09 AM EDT
serviceProxy.ClientCredentials.UserName.UserName = "test";
serviceProxy.ClientCredentials.UserName.Password = "test123";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
string sReturned = serviceProxy.GetData(14);
Eric Bryan18 reputationPosted Jun 14, 2015, 6:14 AM EDT
Thank you very much for your answer Abrar.
I will test your solution tomorrow, on Monday.
Eric Bryan18 reputationPosted Jun 15, 2015, 5:43 AM EDT
Abrar,
I have tested your solution, but it is the same.
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00"
maxReceivedMessageSize="65536"
maxBufferSize="65536"
maxBufferPoolSize="524288"
transferMode="Buffered"
messageEncoding="Text"
textEncoding="utf-8"
bypassProxyOnLocal="false"
useDefaultWebProxy="true" >
contract ="WcfAvecCertificat.IService1">
address="mex" />
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog="300000"
maxSizeOfMessageToLog="200000"
/>
propagateActivity="true" >
initializeData="E:\ebryan\Projets\Wcf\IIS_Logs\messages.svclog" />
Eric Bryan18 reputationPosted Jun 16, 2015, 7:07 AM EDT
I have study how Yaron Naveh's (ClearUsernameBinding) does to do without certificates but it doesn't suit to my needs because the custom binding mode forces to run the service in an application in order to access to the service.
Is there another way to make Wcf authentications without certificates ?
Eric.
Khan Abrar AhmedRank #778 · 3K reputationPosted Jun 16, 2015, 10:59 PM EDT
Khan Abrar AhmedRank #778 · 3K reputationPosted Jun 16, 2015, 11:01 PM EDT
Eric Bryan18 reputationPosted Jun 18, 2015, 12:38 AM EDT
Thank you Abrar,
I will test what you said.