用代码编写服务行为

时间:2017-05-31 10:06:11

标签: c# wcf

我想用C#代码创建服务行为,但我不知道如何。

以下是行为配置:

<?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>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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

ServiceCredentials类位于“System.ServiceModel.Description”中。

我希望这有帮助!