WCF 3.5 UserName身份验证不起作用?

时间:2011-09-26 16:08:46

标签: wcf validation .net-3.5

我要做的是确保我的服务。为此,我使用UserNameAuthentication。我做了绑定,除了某些原因,当我启动服务时,我没有得到验证提示! 验证方法未触发!

这是我的webConfig

我不知道我在这里缺少什么!

  <system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

<services>
  <service behaviorConfiguration="IPhone.Service1Behavior" name="MobileService.IPhone">
    <endpoint address="" binding="wsHttpBinding" contract="MobileService.IIPhone" bindingConfiguration="SafeServiceConf">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="IPhone.Service1Behavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <userNameAuthentication
             userNamePasswordValidationMode="Custom"
             customUserNamePasswordValidatorType="MobileService.CustomValidator, MobileService" />

      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <wsHttpBinding>
    <binding name="SafeServiceConf" maxReceivedMessageSize="65536">
      <readerQuotas maxStringContentLength="65536" maxArrayLength="65536"
         maxBytesPerRead="65536" />
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

以下是我在IPhone.svc中进行验证的代码

我将CustomValidator类放在服务中!

public class CustomValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == "test" && password == "test")
            return;
        throw new SecurityTokenException(
            "Unknown Username or Password");
    }
}

任何帮助?

1 个答案:

答案 0 :(得分:0)

验证提示不会来自WCF。用户界面负责(即ASPX网络表单)。

要将用户名和密码从客户端传递到服务,您可以执行以下操作:

 proxy.ClientCredentials.UserName.UserName = "myUserName";
 proxy.ClientCredentials.UserName.Password = "password";