我有一个使用传输安全模式运行的basichttpbinding WCF服务。效果很好,但是我想对请求此服务的用户进行身份验证,因此尝试使用TransportWithMessagCredential的安全模式和UserName的消息clientCredentialType,并添加了服务行为以调用自定义密码验证器。我的问题是,当我尝试使用Visual Studio调用引用时,出现错误消息,提示找不到该服务。请在下面找到我的代码,并建议我可能需要进行的任何更改才能使其正常运行,并在尝试添加服务时用用户名/密码提示我。

Web.config

<?xml version="1.0"?>
<configuration>
<system.diagnostics>
<sources>
  <source name="System.ServiceModel" switchValue="Information, ActivityTracing"
    propagateActivity="true">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add initializeData="WcfTraces.svclog" type="System.Diagnostics.XmlWriterTraceListener"
        name="traceListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="ServiceModelMessageLoggingListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="c:\users\**\web_messages.svclog"
    type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral,     PublicKeyToken=***"
    name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
</sharedListeners>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
  <messageLogging logEntireMessage="true" logMalformedMessages="true"
    logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
  <basicHttpBinding>
    <binding name="basicBindingConfig">
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="ServiceBehavior" name="nameSpaceX.Service">
    <endpoint address="https://************:443/***.svc/"
      binding="basicHttpBinding" bindingConfiguration="basicBindingConfig"
      contract="nameSpaceY.IService">
      <identity>
        <certificateReference x509FindType="FindByThumbprint" findValue="****************" />
      </identity>
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
        httpsGetUrl="https://*****:443/***.svc" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="namespaceX.UserNameValidator, namespaceX" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>


userNameValidator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;

namespace namespaceX
{
class UserNameValidator: UserNamePasswordValidator
{
    public override void Validate(string username, string password)
    {
        if(username == "test" && password == "1234")
            return;
        throw new SecurityTokenException("Incorrect username or Password");
    }
}
}

最佳答案

您不需要在此指向* .svc的路径。通常httpsGetUrl属性被跳过或存储...?wsdl

<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
  httpsGetUrl="https://*****:443/***.svc" />


这是在假设URL与服务端点中的URL相同的前提下进行的。 Ti应该是不同的URL,因为一个是实现,第二个是元数据。

关于c# - SSL Web服务中的自定义用户名/密码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10998328/

10-11 22:34
查看更多