使用基本身份验证在IIS 7.0服务安全配置问题上的WCF

时间:2012-03-27 08:50:18

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

在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 处理程序StaticFile 错误代码0x80070002 请求的URL https://localhost:9011/FrontEndWS/Account/Login?ReturnUrl=%2fFrontEndWS%2fMeterReadingService.svc

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

你能指出为什么会发生这种情况的指针,或者我如何记录这个错误的原因?

3 个答案:

答案 0 :(得分:1)

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

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

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

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

..和binding.security这样:

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

答案 1 :(得分:0)

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

问题是,在这些配置下面,客户端没有从wcf获取任何数据。

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

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

我的客户端(简单的ASP.net网站调用WCF)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>

答案 2 :(得分:0)

我不知道是什么

<authentication mode="Windows" /> does. 

我在那里添加了jsut。我不知道这是否正确。如我所说;我要做的是通过用户名和密码保护我的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))
        {                                                      

我的问题是被调用两次,第一次有标题,代码可以验证标题,但标题为null的秒时间。不知道为什么会这样吗?

相关问题