【发布时间】: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