ServiceStack:GlobalRequestFilters - 如何使用?似乎没有被解雇

时间:2018-02-07 13:29:00

标签: c# servicestack

我想要一个客户身份验证流程,我只想用[Authenticate]修饰请求来阻止一些安全的。

要检查它们是否经过身份验证,我想基本上只是查找我的onw缓存以查看它们是否已登录(使用http标头中的身份验证令牌)。

我在最后几个小时试图了解如何在ServiceStack中执行此操作,但失败了。

一方面,我尝试使用"插件"并实现一个自定义的CredentialsAuthProvider,但是" TryAuthenticate"也不是"认证"方法永远不会被解雇(我希望Authenticate,因为TryAuthenticate不是我想要的,他们不是试图登录,只是试图访问一个安全的资源:

namespace tWorks.Alfa.Modules.ModuleRestApiService
{
    public class AppHost : AppSelfHostBase
    {
        public AppHost() : base("HttpListener Self-Host", typeof(Services.AlfaProService.AlfaProService).Assembly)
        {
        }

        public override void Configure(Funq.Container container)
        {
            Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[] {
                    new CustomCredentialsAuthProvider(), //HTML Form post of User/Pass
                }
            ));
        }
    }

    public class CustomCredentialsAuthProvider : CredentialsAuthProvider
    {
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            return base.TryAuthenticate(authService, userName, password);
        }
        public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
        {
            return null;
        }
    }
}

在Fiddler尝试这个时,我得到:



HTTP/1.1 401 Unauthorized
Transfer-Encoding: chunked
Vary: Accept
Server: Microsoft-HTTPAPI/2.0
Set-Cookie: ss-pid=iqGwEavfTzBUeiAOKDcw;path=/;expires=Sun, 07 Feb 2038 13:24:02 GMT;HttpOnly
Set-Cookie: ss-id=sPiGKvFkyRvo78yBvqc8;path=/;HttpOnly
X-Powered-By: ServiceStack/5,02 NET45 Win32NT/.NET
WWW-Authenticate: credentials realm="/auth/credentials"
Date: Wed, 07 Feb 2018 13:24:02 GMT

0




在此之后,我尝试使用GlobalRequestFilters,但我也没有采取任何行动。

namespace tWorks.Alfa.Modules.ModuleRestApiService
{
    public class AppHost : AppSelfHostBase
    {
        public AppHost() : base("HttpListener Self-Host", typeof(Services.AlfaProService.AlfaProService).Assembly)
        {
        }

        public override void Configure(Funq.Container container)
        {
            this.GlobalRequestFilters.Add((req, res, requestDto) => 
            {
                // BREAKPOINT NEVER HIT
                string authToken = req.GetHeader("AuthToken");
                res.ReturnAuthRequired();
            });
        }
    }

在Fiddler中,我看到了这一点:



HTTP/1.1 500 No registered Auth Providers found matching any provider
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Accept
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/5,02 NET45 Win32NT/.NET
Date: Wed, 07 Feb 2018 13:27:09 GMT

71
{"ResponseStatus":{"ErrorCode":"Exception","Message":"No registered Auth Providers found matching any provider"}}
0




我该怎么办?

更新 这是RAW HTTP POST调用:

POST http://192.168.0.147:8080/alfaconnect/login/ HTTP/1.1
Host: 192.168.0.147:8080
Accept: application/json
Content-Type: application/json
Content-Length: 38
AuthToken: abcdefgh
DeviceUUID: 123asd123

{"Username":"Ted","Password":"String"}

这是我要保护的测试请求,以及在允许此方法运行之前要执行的自定义身份验证逻辑。 不要介意它被称为"登录",它只是一个虚拟方法,我想测试保护并看到我的自定义代码被执行。

    [Authenticate]
    [Route("/login")]
    public class Login: IReturn<LoginResponse>
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }

    public class LoginResponse
    {
        public string Result { get; set; }
    }

0 个答案:

没有答案