使用来自WPF应用程序的userName和密码的WCF basicHttpBinding

时间:2015-04-10 08:48:04

标签: c# .net wpf wcf basichttpbinding

我在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 app的配置:

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

1 个答案:

答案 0 :(得分:0)

你可以调试“Service.Open()” - 方法吗?

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

所以你需要编写一个扩展ServiceBehavior的CustomBehavior,然后调用四个方法之一(Valdate,AddBindingParameters等...) 您需要找出调用PasswordValidator的位置。

我认为它是在绑定服务的BindingElementCollection的SecurityBindingElement中(哇什么句子)。接下来,您必须添加CustomBehavior(app.config或代码中的dirctly)。 Maby你需要将绑定强制转换为CustomBinding,以便能够更改SecurityBindingelement的元素。

我希望这可以帮助您使用CustomPasswordValidator解决您的问题。