none
Custom authentication in WCF RRS feed

  • Question

  • Hello,

    I want to implement a custom Username and Password validation in my WCF service. The configuration of my web.cofig looks like this:

    <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="ServiceBinding">
          <security mode="Message">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="customBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyWcfService.ClientValidator,MyWcfService"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MyWcfService.Service1" behaviorConfiguration="customBehaviour">
        <endpoint address=""  binding="wsHttpBinding" contract ="MyWcfService.IService1">
        </endpoint>
        <endpoint contract="IMetadataExchange"  binding="mexHttpBinding"  address="mex" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    This is my custom validator:

    public class ClientValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }
    
            if (!(userName == "test" && password == "tset"))
            {
                throw new SecurityTokenException("Unknown Username or Incorrect Password");
            }
        }
    }

    However when calling the service via my test client, the validation does not happen. What do i miss here?

    Thanks in advance,

    Kabid

    Monday, June 18, 2018 8:25 PM

All replies

  • Hi KBid,

    For your issue, I suggest you add the "ServiceBinding" to your endpoint.

    <endpoint address=""  binding="wsHttpBinding" bindingConfiguration="ServiceBinding" contract ="MyWcfService.IService1">
        </endpoint>

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 19, 2018 2:40 AM