FrenkyB
FrenkyB

Reputation: 7197

Write service behavior with code

I would like to create service behavior with C# code, but I don't know how.

Here is configuration of behavior:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ValidateUser">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
            customUserNamePasswordValidatorType="ConsoleApplication1.CustomUserNameValidator"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Upvotes: 0

Views: 194

Answers (1)

Pablo
Pablo

Reputation: 34

try something like this:

var srvCredentials = new ServiceCredentials(); srvCredentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom; (NameOfYourServiceHost).Description.Behaviors.Add(srvCredentials);

The ServiceCredentials class is in "System.ServiceModel.Description".

I hope this helps!

Upvotes: 1

Related Questions