在 IIS 8 上运行的自定义身份验证服务堆栈服务对于未经身份验证的方法返回 404

本文关键字:服务 身份验证 于未 方法 返回 堆栈 运行 自定义 IIS | 更新日期: 2023-09-27 18:37:25

我正在IIS 8,集成管道(框架4.5)上运行我的ServiceStack服务Web项目(非MVC项目)。

现在,如果我的服务尚未通过身份验证,它将返回 404。如果经过身份验证,它将正常运行。我本来以为是401。我还用IIS Express对其进行了测试,并返回了相同的代码。

请求/响应 DTO:

[Route("/common/init/{token}/{timestamp}")]
public class InitRequest
{
    public string Token { get; set; }
    public string TimeStamp { get; set; } //This also prevents an unwanted IE request caching
}
public class InitResponse
{
}

服务:

public class CommonService : Service
{
    [Authenticate]
    public object Get(InitRequest request)
    {
        ...
    }
 }

网络配置:

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
        <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
</system.webServer>  

顺便说一下,我正在使用自定义身份验证,代码:

public class CustomAuthProvider : AuthProvider
{
    public CustomAuthProvider()
    {
        this.Provider = "Custom AuthProvider";
    }
    public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
    {
        return session.IsAuthenticated;
    }
    public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
    {
        throw new NotImplementedException();
    }
}

请求标头:

GET /GlonecoServices/login?      redirect=http%3a%2f%2flocalhost%2fGlonecoServices%2fcommon%2finit%2fsometoken%2f123456789 HTTP/1.1
User-Agent: Fiddler
Host: localhost

响应标头:

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Vary: Accept
Server: Microsoft-IIS/8.5
X-Powered-By: ServiceStack/4,035 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 14 Jan 2015 18:22:20 GMT
Content-Length: 328

在 IIS 8 上运行的自定义身份验证服务堆栈服务对于未经身份验证的方法返回 404

对于HTML客户端/用户代理(即Web浏览器),默认情况下,ServiceStack会自动将尝试访问受[Authenticate]保护服务的未经身份验证的用户重定向到/login页面。

注册 AuthFeature 插件时可以更改/login页面约定,即:

Plugins.Add(new AuthFeature(...) {
    HtmlRedirect = "/customlogin"
});