C# REST webservice认证问题

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

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

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

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

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

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

<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>

最佳答案

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

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

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

希望对您有所帮助。

关于C# REST webservice认证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830696/

相关文章:

c# - 聚合异常 "One or more errors occurred.An error occurred while sending the request."

c# - 单个类项目中的可访问性级别

匹配 Excel 文件扩展名的 C# 正则表达式

asp.net - 使用 IIS Express 的 machine.config 和 processmodel

asp.net - 当用户控件可见时执行 JavaScript

c# - 当我在 Javascript 中禁用它时,ASP.NET 组合框的值似乎发生了变化

c# - 为什么 WCF 中需要 KnownTypeAttribute

c# - 如何无异常(exception)地指示失败的操作(多个应用程序域)?

带有大括号的 C# 文本格式错误

java - IWAB0399E 从 WSDL 生成 Java 时出错 : java. io.IOException:错误:缺少 <soap:fault> 元素 inFault