我有一个使用传输安全模式运行的基本httpbinding WCF 服务。这很好用,但是我想对请求此服务的用户进行身份验证,因此尝试使用 TransportWithMessagCredential 的安全模式和 UserName 的消息 clientCredentialType 并添加了一个服务行为来调用自定义密码验证器。我的问题是,当我尝试使用 Visual Studio 调用引用时,我收到一条错误消息,提示找不到该服务。请在下面找到我的代码,并建议我可能需要进行的任何更改以使其正常工作,并在尝试添加服务时提示我输入用户名/密码。
网页配置
<?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>
用户名验证器.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");
}
}
}