2

你好服务堆栈用户 -

即将到来的问题的 TL;DR 版本是这样的:使用 Servicestack 进行身份验证时,如何返回自定义身份验证响应?

我已经检查了有关此主题的一些以前的 Stack 问题/答案,但我还没有想出最好的方法来做到这一点:

如何扩展 ServiceStack 身份验证 从 ServiceStack 身份验证返回自定义身份验证响应对象

我想将此对象从 ServiceStack 返回到远程 JsonServiceClient:

public class MyGreatCustomAuthResponse : AuthResponse
{
    public UserModel UserModel { get; set; }
}

我确实有一个客户端当前正在使用默认身份验证响应。我这样做是这样的:

var AuthResponse = client.Post(new Auth
{
    provider = "credentials",
    UserName = user.user_id, 
    Password = user.password,
    RememberMe = true
});

我假设接受自定义响应的方式可能如下所示:

var AuthResponse = client.Post<MyGreatCustomResponse>(new CustomAuthRequest
{
    provider = "credentials",
   UserName = user.user_id, 
   Password = user.password,
    RememberMe = true
});
//Use the UserModel object in the response for some other function on the client
var UserModel = AuthResponse.UserModel ; 

这是很容易实现的吗?如果是这样,如何开始编写代码?

4

1 回答 1

0

在 v3 中,除了复制和自定义内置AuthService 之外,您还可以通过使用GlobalResponse Filter Attribute附加自定义 HTTP 标头来添加其他元数据。

在下一个 v4 版本中,我们Dictionary<string, string> Meta身份验证和注册响应 DTO添加了一个,因此您可以使用响应过滤器直接在响应 DTO 中添加元数据。

示例代码

 this.GlobalResponseFilters.Add((req, res, responseDto) =>
        {
            if (res.Dto.GetType() == typeof(AuthenticateResponse))
            {
                CustomUserSession cs = (CustomUserSession)req.GetSession();
                Dictionary<string, string> otherData = new Dictionary<string, string>();
                otherData.Add("CustomData", cs.CustomData.ToString());
                ((AuthenticateResponse)res.Dto).Meta = otherData;
            }
        });
于 2013-10-23T21:18:06.873 回答