【问题标题】:WCF Authentication: Custom Username and Password Validator asp.netWCF 身份验证:自定义用户名和密码验证器 asp.net
【发布时间】:2011-10-23 20:36:44
【问题描述】:

是否需要创建服务证书才能使用自定义用户名和密码验证?我想使用自定义用户名和密码验证我的 WCF 服务。

我的服务web.config如下:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>`enter code here`
            <binding name="NewBinding0">
                <security mode="Message">
                    <transport clientCredentialType="Basic" />
                    <message clientCredentialType="UserName" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="WcfTest.Service1Behavior" name="WcfTest.TestService">
            <endpoint address="" binding="wsHttpBinding" contract="WcfTest.ITestService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="NewBehavior" />
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="WcfTest.Service1Behavior">
                <serviceMetadata httpGetEnabled="false" />
                <serviceDebug includeExceptionDetailInFaults="false" />
                <serviceCredentials>   
                    <!-- Use our own custom validation -->
                    <userNameAuthentication userNamePasswordValidationMode="Custom"
                     customUserNamePasswordValidatorType="MyValidator,WcfTest"/>
                </serviceCredentials>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

而客户端 Web.config 是:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_ITestService" 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" 
                             establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:2374/Service1.svc" binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_ITestService" 
                  contract="ServiceReference1.ITestService"
                  name="WSHttpBinding_ITestService">
            <identity>
                <userPrincipalName value="NYSA31\abc" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

但我在访问服务时遇到以下错误。

【问题讨论】:

  • 您的服务和客户端之间的安全设置不匹配。您的服务正在指定消息安全性,传输设置为 Basic ClientCredentialType,消息设置为 UserName ClientCredentialType,并且您的客户端使用消息安全性,传输设置为 Windows ClientCredentialType。不确定这是否是问题所在,但您可能需要调查该区域。
  • @Tim:感谢您的回复。当我在默认情况下引用服务时,身份验证设置为 windows,尽管我在我的服务中使用用户名作为身份验证类型。我已经对我的客户端 web.config 进行了更正,但仍然出现同样的问题。
  • 因为模式设置为Message,所以TransportClientCreadentialType配置什么并不重要。
  • @Ladislav Mrnka - 我想知道,但没有机会研究答案。将模式设置为Message 是否实质上会使Transport 元素无用?

标签: wcf wcf-security


【解决方案1】:

WsHttpBinding 要求服务证书。 WCF 4(以及具有特殊 KB 的旧版本)允许公开使用用户名和密码进行身份验证的服务而无需证书,但您真的想要它吗?这意味着用户名和密码将以纯文本形式通过网络传输 = 没有安全性,因为任何将捕获数据包的人都可以使用被盗凭据进行身份验证。

要使用没有证书的用户名密码,您需要custom binding,或者您可以使用ClearUserNameBinding

【讨论】:

    猜你喜欢
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多