【问题标题】:WCF basicHttpBinding with userName and password from a WPF appWCF basicHttpBinding 来自 WPF 应用程序的用户名和密码
【发布时间】:2015-04-10 08:48:03
【问题描述】:

我在 SO 中阅读了一些问题和一些链接教程。

我已经做了所有的事情,但是我有一个问题......身份验证不起作用......只有当我发送随机用户名时它才会给出异常,而不考虑密码。

我想,也许是因为我写的验证器从来没有被调用过(我已经给它设置了断点)......但是为什么呢?

这是我对 wcf 的 app.confing:

<appSettings>
  <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  <add key="userName" value="piero"/>
  <add key="password" value="piero"/>
</appSettings>
<system.web>
  <compilation debug="true" />
</system.web>
<!-- Quando si distribuisce il progetto della libreria di servizi, è necessario aggiungere il contenuto del file di configurazione al file 
app.config dell'host. System.Configuration non supporta i file di configurazione delle librerie. -->
<system.serviceModel>
  <!--<standardEndpoints/>-->
  <bindings>
    <basicHttpBinding>
      <binding name="usernameHttps">
        <security mode="TransportWithMessageCredential">
          <message clientCredentialType="UserName"/>
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <services>
    <service name="calc.CalcService" behaviorConfiguration="customBehavior">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
        name="base" contract="calc.ICalcService"  >
        <identity>
          <dns value="localhost"  />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      <endpoint address="web" behaviorConfiguration="webHttp" binding="webHttpBinding"
        bindingConfiguration="" name="web" contract="calc.ICalcService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8733/Design_Time_Addresses/calc/" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttp">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
          <serviceBehaviors>
    <behavior >

      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>


      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
    <behavior name="customBehavior">
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
      <serviceCredentials>
        <userNameAuthentication
          userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="calc.CustomPass, calc"/>
      </serviceCredentials>
    </behavior>

    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="webHttpBinding" scheme="http"  />
  </protocolMapping>
</system.serviceModel>

这是调用 wpf 应用程序的配置:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
      <startup> 
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <system.serviceModel>
          <bindings>
              <basicHttpBinding>
                  <!--<binding name="base" />-->
                <binding name="base">
                  <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Basic"/>
                  </security>
                </binding>
              </basicHttpBinding>
          </bindings>
          <client>
              <endpoint address="http://localhost:8733/Design_Time_Addresses/calc/"
                  binding="basicHttpBinding" bindingConfiguration="base" contract="calcTest.ICalcService"
                  name="base" />
          </client>
      </system.serviceModel>
    <appSettings>
      <add key="userName" value="aaa"/>
      <add key="password" value="bbb"/>
    </appSettings>
  </configuration>

这是 wcf 的验证器:

public class CustomPass : UserNamePasswordValidator //System.IdentityModel.dll
{
    private const string USERNAME_ELEMENT_NAME = "userName";

    private const string PASSWORD_ELEMENT_NAME = "password";

    private const string FAULT_EXCEPTION_MESSAGE = "UserName or Password is incorrect!";

    public override void Validate(string userName, string password)
    {
        //Guarder.Guard.ArgumentNotNull(userName).ArgumentNotNull(password);
        var validateUserName = ConfigurationManager.AppSettings[USERNAME_ELEMENT_NAME];
        var validatePassword = ConfigurationManager.AppSettings[PASSWORD_ELEMENT_NAME];
        var validateCondition = userName.Equals(validateUserName) && password.Equals(validatePassword);
        if (!validateCondition)
        {
            throw new FaultException(FAULT_EXCEPTION_MESSAGE);
        }
    }
}

所以:

  • wcf 服务正常启动;
  • wpf 应用程序调用它(basicHttpBinding)并使用它,但是:

    1. 如果没有用户名,它会从 wcf 得到错误(我认为这没关系)

    2. wcf 服务接受所有用户名,不仅是我在验证器中设置的用户名

    3. 密码没用,什么都可以发,但好像只有用户名

以某种方式阅读,但 wcf

如何使用我的验证器?为什么不启动?

我以这种方式调用 wcf(这里有什么错误吗?):

private void Add(object sender, RoutedEventArgs e)
{
    var n1 = this.opOne.Text.ToString();
    var n2 = this.opTwo.Text.ToString();
    var calc = new calcTest.CalcServiceClient();
    calc.ClientCredentials.UserName.UserName = "piero";
    calc.ClientCredentials.UserName.Password = "piero";
    this.res.Text = calc.Add(n1, n2);
}

【问题讨论】:

  • 你遇到了什么异常?
  • 我现在在 basicHttpBinding 端点中添加了这个:behaviorConfiguration="customBehavior",它说它不是有效值,但它是我的自定义行为的名称...
  • 它甚至不是一个例外..在Visual Studio编辑器中,它带有蓝色下划线..并且消息说:“属性behaviorConfiguration无效。值customBehavior对endpointBehaviorConfigurationType无效数据类型”
  • 好的,好的,这是固定的..但它仍然没有调用我的验证器!

标签: c# .net wpf wcf basichttpbinding


【解决方案1】:

你能调试一下“Service.Open()” - 方法吗?

在那里,您将看到您的 CustomBehavior 是否已添加到服务的行为中。您必须调用 Behavior-Methods,并调用 CustomPasswordValidator。

所以您需要编写一个自定义行为来扩展 ServiceBehavior,然后调用四种方法之一(Valdate、AddBindingParameters 等...) 您需要找出调用 PasswordValidator 的位置。

我认为是在SecurityBindingElement 的BindingElementCollection 中绑定了你的服务(哇什么一句话)。接下来,您必须添加 CustomBehavior(app.config 或直接在代码中)。您可能需要将绑定强制转换为 CustomBinding 才能更改 SecurityBindingelement 的元素。

我希望这将帮助您解决有关 CustomPasswordValidator 的问题。

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多