【问题标题】:How to give access to WCF web service hosted on IIS only for specific users?如何仅为特定用户授予对托管在 IIS 上的 WCF Web 服务的访问权限?
【发布时间】:2011-10-28 11:57:08
【问题描述】:

我有一个使用 Windows 身份验证的 Web 服务。 Web 服务托管在 IIS 上。是否可以将对该 Web 服务的访问范围缩小到少数特定用户?我当前的网络配置:

<services>
  <service name="LANOS.SplunkSearchService.SplunkSearch">
    <endpoint binding="basicHttpBinding" bindingConfiguration="basicHttp"
      contract="LANOS.SplunkSearchService.ISplunkSearch" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true" maxBufferSize="20000000"
      maxBufferPoolSize="20000000" maxReceivedMessageSize="20000000">
      <readerQuotas maxDepth="32" maxStringContentLength="200000000"
        maxArrayLength="200000000" />
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

顺便说一下,我尝试了一个类似的解决方案,这是我在网上找到的:

    <authentication mode="Windows"/>

    <authorization>

          <allow roles=".\Developers"/>

          <allow users="DOMAIN\ServiceAccount"/>

          <deny users="*"/>

    </authorization>

但它不起作用。 :( 它让所有域用户通过。

【问题讨论】:

  • 您是否为在 IIS 中托管 WCF 服务的应用程序禁用了匿名身份验证并启用了 Windows 身份验证?

标签: c# wcf web-services


【解决方案1】:

Wrox 的Professional WCF 4 描述了一种在 Ch. 中设置用户名/密码身份验证的方法。 8. 总而言之,您需要一个自定义验证器:

public class MyCustomUserNamePasswordValidator : UserNamePasswordValidator
{
   public override void Validate(string userName, string password)
   {
     if(userName != "foo" && password != "bar") 
     {
        throw new SecurityTokenValidationException("Invalid user");
     }
   }
}

之后,您需要将服务配置中的 userNameAuthentication 元素修改为“自定义”并定义验证器:

 <userNameAuthentication
      userNamePasswordValidationMode = "Custom"
          customUserNamePasswordValidatorType =
          "Common.MyCustomUserNamePasswordValidator, Common" />

我希望这会有所帮助。

【讨论】:

  • Okey 我会尝试,但我不相信你不能在不修改 WS 代码的情况下真正让它工作。我仍然希望我只能通过更改网络配置来让它工作。
猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
相关资源
最近更新 更多