我创建了一个托管在 IIS 中的简单 WCF 服务。现在我想使用我自己的用户名身份验证。 我的 web.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfIIsBasicAuthTest.Service1" behaviorConfiguration="MyBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="WcfIIsBasicAuthTest.IService1"
bindingConfiguration="MyBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfIIsBasicAuthTest.MyValidator, WcfIIsBasicAuthTest"/>
</serviceCredentials>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
验证者
namespace WcfIIsBasicAuthTest
{
public class MyValidator :UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if(!(userName == "test" && password == "test"))
{
throw new SecurityTokenException("Validation Failed!");
}
}
}
}
如果我从 visual studio 中启动此 WCF 服务,我会收到以下错误:The authentication schemes configured on the host ('Ntlm, Anonymous') do not allow those configured on the binding 'BasicHttpBinding' ('Basic').
如果我尝试连接到托管在 IIS 中的服务,我会收到错误消息,具体取决于设置的身份验证类型,但它根本不起作用。
如果仅启用匿名身份验证则出错:The authentication schemes configured on the host ('Anonymous') do not allow those configured on the binding 'BasicHttpBinding' ('Basic').
如果我在 IIS 中设置基本身份验证,它需要一个我不想提供的有效本地用户(因为我想编写自己的用户提供程序)。
关于如何使用 wcf 和 iis 设置这样一个基本的 auth userprovider 的任何提示/链接?
最佳答案
你可以设置下面的配置来使用你自己的 UserNameValidator:
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
要使其与 basicHttpBinding 一起使用,您需要设置 HTTPS,因为 WCF 不允许以明文形式通过 channel 传递用户名密码。另一种选择是使用 wsHttpBinding。
关于c# - 我必须使用哪些 IIS 设置才能通过 WCF 使用自己的密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9175322/