【问题标题】:WCF 3.5 UserNameAuthentication is not working?WCF 3.5 UserNameAuthentication 不起作用?
【发布时间】:2011-09-26 16:08:45
【问题描述】:

我要做的是保护我的服务。为此,我使用了 UserNameAuthentication。我进行了绑定和所有操作,但由于某些原因,当我启动服务时,我没有收到验证提示! Validate 方法没有被触发!

这是我的网络配置

我不知道我在这里错过了什么!

  <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");
    }
}

有什么帮助吗?

【问题讨论】:

    标签: wcf validation .net-3.5


    【解决方案1】:

    验证提示不会来自 WCF。 UI 对此负责(即 ASPX 网络表单)。

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

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

    【讨论】:

    • 我只想保护 .svc 文件!我可以从客户端提示,但是如果有人直接访问该 URL,他们可以访问该服务吗?
    • 不,如果您使用 UserNamePasswordValidator 并且他们没有传递正确的 ClientCredentials(即用户名和密码),用户将无法调用您的服务。
    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多