如何通过Castle Windsor传递Username Credential WCF

时间:2016-10-25 03:46:00

标签: c# wcf castle-windsor

我在使用自定义WCF身份验证通过Castle Windsor注册表传递用户名和密码时遇到问题。

我有如下的Web服务配置:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsSecureBinding">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="defaultProfile" name="GS1.CSSM.XMLGenerate.WebService.Implementation.XMLGenerateService">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsSecureBinding" name="wsSecureService" contract="GS1.CSSM.XMLGenerate.WebService.Interface.IXMLGenerateService">        
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="defaultProfile">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="GS1.CSSM.XMLGenerate.WebService.ServiceAuthenticator, GS1.CSSM.XMLGenerate.WebService"/>
          </serviceCredentials>
        </behavior>
        <behavior name="noAuthentication">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>        
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

这是容器设置:

var container = new WindsorContainer();

container.AddFacility()
                .Register(
                    Component.For<IXMLGenerateService>()
                        .ImplementedBy<XMLGenerateServiceClient>()
                        .AsWcfClient(
                            new DefaultClientModel
                            {
                                Endpoint =
                                    WcfEndpoint.FromConfiguration("wsSecureService")
                            }.Credentials(new UserNameCredentials("zul", "password"))));

这是客户端配置文件:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsSecureService">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost:44302/Service/XMLGenerateService.svc" binding="wsHttpBinding" bindingConfiguration="wsSecureService" contract="XMLGenerateService.IXMLGenerateService" name="wsSecureService" />
    </client>
  </system.serviceModel>

我收到此错误“其他信息:未提供用户名。请在ClientCredentials中指定用户名。”一旦我试图从服务中调用方法。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我会自己回答:)

这就是我如何解决这个问题

container.Kernel.AddFacility<WcfFacility>()
            .Register(Component.For<IXMLGenerateService>()
            .AsWcfClient(new DefaultClientModel
            {
                Endpoint = WcfEndpoint.FromConfiguration("wsSecureService")
            }.Credentials(new UserNameCredentials("zul", "password"))));

        _smXMLGenerateService = container.Resolve<IXMLGenerateService>();

希望它可以帮助任何有同样问题的人。