c# - 我必须使用哪些 IIS 设置才能通过 WCF 使用自己的密码验证

标签 c# wcf iis

我创建了一个托管在 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/

相关文章:

c# - 文件夹和文件未添加到用户的应用程序数据文件夹

c# - 在 Adob​​e AxAcroPDFLib 中启用注释

iis - 我可以使用 PowerShell 而不是 AppCmd.exe 来监视 IIS 状态吗?

security - 客户端证书映射认证HTTP错误401.2和登录错误0x80090325

iis - 在 IIS 中禁用 HTTP OPTIONS、TRACE、HEAD、COPY 和 UNLOCK 方法

C# 数组或其他数据结构

c# - 与 MimeMapping.GetMimeMapping 相反?

c# - WCF 将额外的 HTTP header 添加到 HTTP 响应以传输 SOAP 消息

wcf - WCF 响应中的 MustUnderstand 属性导致 Java/Axis 客户端出错

wcf - 生产服务器上的偶尔错误 : Method <name> is not supported on this proxy