【问题标题】:C# REST webservice authentication problemC# REST webservice 身份验证问题
【发布时间】:2010-09-30 13:01:13
【问题描述】:

在我之前的问题here 中,我在验证网络服务时遇到了困难。通过使用我发现here 的 WcfRestContrib 库,我能够解决这个问题。我构建了一个小型测试应用程序,身份验证就像一个魅力。

但是,当我在要使用 web 服务身份验证部分的 web 应用程序中实现此功能时,我不断遇到问题,即 web 应用程序中使用的表单身份验证不断将我重定向到登录页面。

我的 web 应用程序的 web.config 中有以下配置部分。这是我试图通过它的 url 调用服务的应用程序;

http://website.localhost/Services/Info.svc/account

web.config 的 website.localhost 包含以下部分;

<location path="Services">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

<system.serviceModel>
  <extensions>
      <behaviorExtensions>
        <add name="webAuthentication" type="WcfRestContrib.ServiceModel.Configuration.WebAuthentication.ConfigurationBehaviorElement, WcfRestContrib, Version=1.0.6.107, Culture=neutral, PublicKeyToken=89183999a8dc93b5"/>
        <add name="errorHandler" type="WcfRestContrib.ServiceModel.Configuration.ErrorHandler.BehaviorElement, WcfRestContrib, Version=1.0.6.107, Culture=neutral, PublicKeyToken=89183999a8dc93b5"/>
        <add name="webErrorHandler" type="WcfRestContrib.ServiceModel.Configuration.WebErrorHandler.ConfigurationBehaviorElement, WcfRestContrib, Version=1.0.6.107, Culture=neutral, PublicKeyToken=89183999a8dc93b5"/>
      </behaviorExtensions>
  </extensions>
  <behaviors>
      <serviceBehaviors>
        <behavior name="Rest">
          <webAuthentication requireSecureTransport="false" authenticationHandlerType="WcfRestContrib.ServiceModel.Dispatcher.WebBasicAuthenticationHandler, WcfRestContrib" usernamePasswordValidatorType="CMS.Backend.Services.SecurityValidator, CMS.Backend" source="CMS.Backend"/>
          <errorHandler errorHandlerType="WcfRestContrib.ServiceModel.Web.WebErrorHandler, WcfRestContrib"/>
          <webErrorHandler returnRawException="true" logHandlerType="CMS.Backend.Services.LogHandler, CMS.Backend" unhandledErrorMessage="An error has occured processing your request. Please contact technical support for further assistance."/>
        </behavior>
      </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

我通过授予所有匿名用户访问权限来将服务目录排除在身份验证之外,我认为这是导致问题的部分。

我的服务(信息)包含以下属性

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceConfiguration("Rest", true)]
public class Info : IInfo
{
   //Some foo hapens
}

服务的 web.config 包含这个;

<system.web>
   <authorization>          
     <allow users="*" />
   </authorization>
</system.web>

每当我尝试使用上面提供的 url 来调用服务时,我都会被重定向到http://website.localhost/Logon 上的网站登录页面。我怎样才能防止这种情况发生?据我所知,web.config 应该是正确的。

--最终解决方案--

我将 web.config 修改为如下所示;

<sytem.web>
  //site config
</system.web>

<location inheritInChildApplications="false">
    <system.web>
      <authentication mode="Forms">
        <forms name="AllPages" loginUrl="~/Logon/" timeout="360" enableCrossAppRedirects="false" />
      </authentication>
    </system.web>
  </location>

我还从之前添加的 web.config 中删除了此规则。显然它与添加的位置标签和服务本身中的 web.config 冲突

<location path="Services" >
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
  </location>

【问题讨论】:

    标签: c# asp.net wcf web-services authentication


    【解决方案1】:

    您应该防止继承相关配置部分中指定的设置并由驻留在相关应用程序的子目录中的应用程序继承。

    尝试将您的网站“system.web”部分放入“位置”部分:

    <location inheritInChildApplications="false">
      <system.web>
        <!-- your site system web settings -->
      </system.web>
    </location>
    

    希望对您有所帮助。

    【讨论】:

    • 感谢您的快速回复。我会试试这个,让你知道结果! :-)
    • 不幸的是,这不起作用。当尝试通过 url 访问服务时,我仍然被重定向到 website.localhost 的登录页面。即使在登录之后。您还有其他建议吗?
    • 请注意,我在子目录中使用服务(.svc 文件)。子目录中的服务是我试图通过 url 访问的。
    • 好吧.. 我有一个类似的问题,并在将我的网站(不是服务)'system.web' 部分放入具有特定属性'inheritInChildApplications="false"' 的'location' 中时解决了它,这意味着做不继承子应用程序的站点安全性(站点安全性应在“system.web”部分中描述) - 在您的案例服务中。可能在您的 IIS 上有一些特定的设置。尝试发布整个站点和子服务配置文件。
    • 感谢您的大力帮助!我花了一段时间才把所有的配置都弄好。但是按照您的建议添加位置就可以了。我将使用最终解决方案更新问题。
    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多