1

我正在尝试使 Servicestack Credentials 身份验证正常工作,但是当我尝试使用装饰有的受保护服务时,[Authenticate]即使身份验证成功,我也会收到未经授权的异常。这里错过了什么?

AppHost 看起来像这样:

    AuthFeature authFeature = new AuthFeature(() => new AuthUserSession(),
            new IAuthProvider[]{                    
                new BpeCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials                                        
            }) { HtmlRedirect = null };
        authFeature.ServiceRoutes[typeof(AuthService)] = new[] { "/auth" };  
        authFeature.IncludeAssignRoleServices = false;
        Plugins.Add(authFeature);
container.Register<ICacheClient>(new MemoryCacheClient());

我认为我TryAuthenticate正确地覆盖并且从 Swagger 一切正常。问题在于客户端调用 - 在针对 myurl/auth/ 的身份验证成功之后 - 对不同服务的下一次调用失败并出现未经授权的异常。请问有什么想法吗?

经过更多研究:看起来需要将 ss-id/ss-pid cookie 传递给受限资源以进行身份​​验证。我怎样才能做到这一点?

4

2 回答 2

0

通过 Ajax 进行身份验证时,cookie 会自动发送给后续请求。

与C#/.NET 服务客户端类似,当指定验证请求时,cookie 将保留在经过验证的服务客户端上RememberMe=true,例如:

var client = new JsonServiceClient(baseUrl);

var authResponse = client.Post(new Authenticate {
    provider = CredentialsAuthProvider.Name,
    UserName = "user",
    Password = "p@55word",
    RememberMe = true,
});

然后,您可以重新使用相同的实例来发出经过身份验证的请求:

var response = client.Post(new Secure { ... });

您还可以通过分配CookieContainer例如:

newClient.CookieContainer = client.CookieContainer;
于 2014-10-22T21:51:35.707 回答
0

我发现不仅需要将 sessionId cookie 传递给后续请求,还需要将整个容器 -ss-id、ss-pid、ss-opt、X-UAId-。我无法从 ICacheClient 访问 cookie 容器,我必须在身份验证完成时手动设置 cookie,并请求该容器的所有 cookie 并将它们添加到新的服务调用中。

于 2014-10-29T13:57:00.780 回答