2
votes

I have a big problem with custom user name authentication in WCF. I've made a simple WCF Service which needs users validation via CustomUserValidator. When I log-in from Console Application like this:

using (var svc = new ServiceReference2.DBServiceClient())
{
    Console.WriteLine("Username: ");
    string user = Console.ReadLine();
    Console.WriteLine("Pass: ");
    string pass = Console.ReadLine();
    svc.ClientCredentials.UserName.UserName=user;
    svc.ClientCredentials.UserName.Password=pass;
    Console.WriteLine(svc.GetAllDepartmentList().First().DepartmentID);
    Console.Read();
}

Everything is fine, but when I want to use my GUI Client which essentialy do the same I've got an error: Outer Exception:

Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint.

Inner Exception:

The request for security token has invalid or malformed elements.

The app.config entry for both of them is identical:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IDBService" 
        closeTimeout="00:01:00" openTimeout="00:01:00" 
        receiveTimeout="00:10:00" sendTimeout="00:01:00"
        bypassProxyOnLocal="false" transactionFlow="false"
        hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
        allowCookies="false">
        <readerQuotas maxDepth="32" maxStringContentLength="8192"
          maxArrayLength="16384" maxBytesPerRead="4096"
          maxNameTableCharCount="16384" />
        <reliableSession ordered="true" 
          inactivityTimeout="00:10:00" enabled="false" />
        <security mode="Message">
          <transport clientCredentialType="Windows" 
            proxyCredentialType="None" realm="" />
          <message clientCredentialType="UserName" 
            negotiateServiceCredential="true" algorithmSuite="Default" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint name="WSHttpBinding_IDBService"
      address="http://localhost:8732/Design_Time_Addresses/DBService/Service1/"
      binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_IDBService"
      contract="ServiceReference2.IDBService" >
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </client>
</system.serviceModel>

Thanks for all your help!

Edit: posting GUI code in which I catch an exception:

            using (var svc = new DBServiceHost.DBServiceClient())
        {
            svc.ClientCredentials.UserName.UserName = view.userName;
            svc.ClientCredentials.UserName.Password = view.userPass;
            int asd = svc.GetCompanies().First().CompanyID;                
        }

This assigment to int is just to get this exception in this place.

1
Can you post your GUI's code just in case there's something subtle there making the difference? - Jacob
In your console app you are creating ServiceReference2.DBServiceClient client but in your UI app you are creating DBServiceHost.DBServiceClient. Does your config reflect these name changes? - Ladislav Mrnka
yes, DBServiceHost is the reference to the same service, just in UI app I make it a name and console app was done fast with standard names - Pax0r

1 Answers

0
votes

Try running Gui app with right click Run As Admin. Also make sure that you are trimming the view.userName fields. The trailing whitespace can create problem.