C# 如何仅为特定用户授予对托管在IIS上的WCF web服务的访问权限?

C# 如何仅为特定用户授予对托管在IIS上的WCF web服务的访问权限?,c#,wcf,web-services,C#,Wcf,Web Services,我有一个使用Windows身份验证的web服务。Web服务托管在IIS上。是否有可能将对该web服务的访问范围仅限于少数特定用户?我当前的web配置: <services> <service name="LANOS.SplunkSearchService.SplunkSearch"> <endpoint binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="

我有一个使用Windows身份验证的web服务。Web服务托管在IIS上。是否有可能将对该web服务的访问范围仅限于少数特定用户?我当前的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>


但它不起作用(它允许所有域用户通过。

Wrox在第8章中介绍了一种设置用户名/密码身份验证的方法。总而言之,您需要一个自定义验证器:

public class MyCustomUserNamePasswordValidator : UserNamePasswordValidator
{
   public override void Validate(string userName, string password)
   {
     if(userName != "foo" && password != "bar") 
     {
        throw new SecurityTokenValidationException("Invalid user");
     }
   }
}
 <userNameAuthentication
      userNamePasswordValidationMode = "Custom"
          customUserNamePasswordValidatorType =
          "Common.MyCustomUserNamePasswordValidator, Common" />
之后,您需要将服务配置中的userNameAuthentication元素添加到“Custom”并定义验证器:

public class MyCustomUserNamePasswordValidator : UserNamePasswordValidator
{
   public override void Validate(string userName, string password)
   {
     if(userName != "foo" && password != "bar") 
     {
        throw new SecurityTokenValidationException("Invalid user");
     }
   }
}
 <userNameAuthentication
      userNamePasswordValidationMode = "Custom"
          customUserNamePasswordValidatorType =
          "Common.MyCustomUserNamePasswordValidator, Common" />


我希望这会有所帮助。

您是否已禁用匿名身份验证,并为IIS中承载WCF服务的应用程序启用Windows身份验证?好的,我会尝试,但我不相信如果不修改WS-code,您实际上无法使其工作。我仍然希望仅通过更改web配置才能使其工作。