【问题标题】:WCF on IIS 7.0 Service Security Configuration Issue using Basic Authentication使用基本身份验证的 IIS 7.0 服务安全配置问题上的 WCF
【发布时间】:2012-04-10 18:49:28
【问题描述】:

过去几天我一直在尝试解决在 SQL 2008 Server 和 IIS 7.0 中托管 WCF 服务时出现的问题。

仅当我禁用匿名身份验证并使用基于 SSL 的基本身份验证时才会出现此问题,如下面的 web.config 摘录中所示

<security mode="Transport">
    <transport clientCredentialType="Basic" />
</security>

基本上,当我尝试访问 MeterReadingService.svc 文件时,我收到一个 IIS 错误,提示它找不到资源 \Account\Login。 似乎我被重定向到登录页面。 IIS 上未启用表单身份验证

错误详情:

HTTP 错误 404.0 - 未找到 您要查找的资源已被删除、名称已更改或暂时不可用。

详细的错误信息 模块 IIS Web 核心 通知 MapRequestHandler 处理程序静态文件 错误代码 0x80070002 请求的网址 https://localhost:9011/FrontEndWS/Account/Login?ReturnUrl=%2fFrontEndWS%2fMeterReadingService.svc

物理路径 D:\WebApplications\MeterReaderPortal\FrontEndWS\Account\Login 登录方法基本 登录用户管理员

您能否指出为什么会发生这种情况或如何记录此错误的原因?

【问题讨论】:

    标签: asp.net wcf iis-7 forms-authentication basic-authentication


    【解决方案1】:

    您在 web.config 中的 system.web 下配置了哪些 authentication mode

    根据您发布的错误消息,我假设它是Forms

    要使用带有 Windows 密码的基本身份验证,System.Web.Authentication 需要如下所示:

    <system.web>
      <authentication mode="Windows" />
    </system.web>
    

    ..和binding.security 像这样:

    <security mode="Transport">
      <transport clientCredentialType="Basic" />
    </security>
    

    【讨论】:

      【解决方案2】:

      我希望客户端(它是一个 asp.net 站点)调用 wcf。两者都托管在我笔记本电脑的 IIS7 中。对于 WCF,我在 IIS7 中启用了基本身份验证并禁用了所有其他功能。对于客户端,我启用了基本身份验证并禁用了身份验证中的所有其他选项。

      问题在于,使用下面的这些配置,客户端无法从 wcf 获取任何数据。

      如果我在浏览器中通过向服务提供 url 导航到服务,那么我必须在弹出的表单中输入用户名/密码。

      我要做的是通过用户名和密码保护我的 wcf,以便任何想要调用我的 wcf 的人都必须提供用户名/密码,并且我的 wcf 代码检查是否存在(例如在 AD 中)并进行相应的身份验证.

      我的客户端(调用 WCF 的简单 ASP.net 网站)web.config:

      <configuration>
      <appSettings>
      <add key="userName" value="Administrator"/>
      <add key="password" value="pass"/>
      <!--<add key="url" value="http://localhost:57895/ListData.svc"/>-->
      <!--http://localhost/WCF/-->    
      <add key="url" value="http://localhost:8082/ListData.svc"/>
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.webServer>
        <directoryBrowse enabled="true"/>
      </system.webServer>
      </configuration>
      

      我的 WCF 服务 web.config:

      <?xml version="1.0"?>
      <configuration>
      <appSettings>
      <add key="Logging" value="true"/>
      </appSettings>
      <connectionStrings/>
      <system.web>
      <authentication mode="Windows"></authentication>
      <compilation debug="true" />
      <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
      </system.web>
      <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <binding name="httpBinding">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Basic" />
            </security>
          </binding>
        </basicHttpBinding>
        <!--<webHttpBinding>
          <binding name="webHttpTransportSecurity">
            <security mode="Transport">
              <transport clientCredentialType="Basic"/>
            </security>
          </binding>
        </webHttpBinding>-->
      </bindings>
      <services>
        <service behaviorConfiguration="SecureRESTSvcTestBehavior" name="Hdir.ListData">
          <!--<host>
            <baseAddresses>
              <add baseAddress="http://localhost:57895/ListData/"/>
            </baseAddresses>
          </host>-->
          <!--webHttpBinding allows exposing service methods in a RESTful manner-->
          <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding"      behaviorConfiguration="webHttpBehavior" contract="Hdir.IListData"/>
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <!--<behavior name=" ">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
          </behavior>-->
          <behavior name="SecureRESTSvcTestBehavior">
            <!-- To avoid disclosing metadata information, set the value below to 
                 false and remove the metadata endpoint above before deployment -->
            <serviceMetadata httpGetEnabled="true"/>
            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  
                 Set to false before deployment to avoid disclosing exception information -->
            <serviceDebug includeExceptionDetailInFaults="false"/>
            <serviceAuthorization serviceAuthorizationManagerType="Hdir.CustomAuthorizationManager, Hdir"/>
            <!--<serviceCredentials>
              <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Hdir.Hp.Data.CustomUserNameValidator, Hdir.Hp.Data" />
            </serviceCredentials>-->
          </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
          <behavior name="webHttpBehavior">
            <!--<webHttp/>-->
          </behavior>
        </endpointBehaviors>
      </behaviors>
      </system.serviceModel>
      <system.webServer>
      <directoryBrowse enabled="true"/>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*"/>
          <add name="Access-Control-Allow-Headers" value="x-requested-with"/>
          <add name="Access-Control-Request-Method" value="GET"/>
        </customHeaders>
      </httpProtocol>
      </system.webServer>
      </configuration>
      

      【讨论】:

        【解决方案3】:

        不知道是什么

        <authentication mode="Windows" /> does. 
        

        我已经把它加在那里了。我不知道这样做是否正确。如我所说;我要做的是通过用户名和密码保护我的 wcf,以便任何想要调用我的 wcf 的人(在互联网上的任何地方)都必须提供用户名/密码,并且我的 wcf 代码检查是否存在(例如在 AD 中)并相应地进行身份验证。我不知道使用 wshttpbinding、webhttpbinding 或 basichttpbinding 使用哪种类型的绑定。在您在配置文件中看到的类 CustomAuthorizationManager 中,我有一个名为

        的覆盖方法
        protected override bool CheckAccessCore(OperationContext operationContext)
            {
                //Extract the Authorization header, and parse out the credentials converting the Base64 string:
                var authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];
                if ((authHeader != null) && (authHeader != string.Empty))
                {                                                      
        

        我的问题是被调用了两次,第一次是有标题并且代码可以验证标题,但第二次标题为空。有谁知道这是为什么?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-21
          • 1970-01-01
          • 2011-07-31
          • 2011-10-25
          相关资源
          最近更新 更多