gpt4 book ai didi

C# REST webservice认证问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:14 24 4
gpt4 key购买 nike

在我之前的问题中 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com